LeetCode 2278: Percentage of Letter in String Solution
Master LeetCode problem 2278 (Percentage of Letter in String), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2278. Percentage of Letter in String
Problem Explanation
Explanation:
To solve this problem, we need to iterate through the given string s and count the number of occurrences of the character letter. Then we calculate the percentage of occurrences of letter in s and round it down to the nearest whole percentage.
- Initialize a variable
countto store the number of occurrences ofletter. - Iterate through each character in the string
s:- If the character is equal to
letter, increment thecount.
- If the character is equal to
- Calculate the percentage of occurrences of
letterinsby dividingcountby the length ofsand multiplying by 100. - Return the rounded down value of the percentage.
:
Solution Code
class Solution {
public int percentageOfLetterInString(String s, char letter) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == letter) {
count++;
}
}
return count * 100 / s.length();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2278 (Percentage of Letter in String)?
This page provides optimized solutions for LeetCode problem 2278 (Percentage of Letter in String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2278 (Percentage of Letter in String)?
The time complexity for LeetCode 2278 (Percentage of Letter in String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2278 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2278 in Java, C++, or Python.