Given a string s
and a character letter
, return the percentage of characters in s
that equal letter
rounded down to the nearest whole percent.
Example 1:
Input: s = "foobar", letter = "o" Output: 33 Explanation: The percentage of characters in s that equal the letter 'o' is 2 / 6 * 100% = 33% when rounded down, so we return 33.
Example 2:
Input: s = "jjjj", letter = "k" Output: 0 Explanation: The percentage of characters in s that equal the letter 'k' is 0%, so we return 0.
Constraints:
1 <= s.length <= 100
s
consists of lowercase English letters.letter
is a lowercase English letter.The problem asks to calculate the percentage of a specific character within a given string, rounded down to the nearest whole number. The solution involves a straightforward approach: counting the occurrences of the character and then calculating the percentage.
Algorithm:
Count Occurrences: Iterate through the input string s
and count the number of times the specified character letter
appears. This can be efficiently done using built-in string functions like count()
(Python), ranges::count()
(C++), or manual iteration.
Calculate Percentage: Divide the count of the character by the total length of the string (len(s)
). Multiply the result by 100 to obtain the percentage.
Round Down: Use floor division (//
in Python, /
in Java/C++/Go when the operands are integers, Math.floor()
in JavaScript/TypeScript) to round the percentage down to the nearest whole number.
Time and Space Complexity Analysis:
Time Complexity: O(n), where n is the length of the input string s
. This is because we iterate through the string once to count the occurrences of the character.
Space Complexity: O(1). The algorithm uses a constant amount of extra space, regardless of the input string's size. We only need to store the count of the character and the final percentage.
Code Examples (with explanations):
Python:
class Solution:
def percentageLetter(self, s: str, letter: str) -> int:
"""
Calculates the percentage of a given letter in a string.
Args:
s: The input string.
letter: The character to count.
Returns:
The percentage of the character in the string, rounded down.
"""
count = s.count(letter) #Efficiently counts occurrences using built-in function.
return (count * 100) // len(s) #Floor division for rounding down.
Java:
class Solution {
public int percentageLetter(String s, char letter) {
int count = 0;
for (char c : s.toCharArray()) { //Iterates through the string character by character.
if (c == letter) {
count++;
}
}
return (count * 100) / s.length(); // Integer division automatically rounds down.
}
}
C++:
#include <string>
#include <algorithm> // for count
class Solution {
public:
int percentageLetter(string s, char letter) {
int count = std::count(s.begin(), s.end(), letter); //Efficient count using algorithm library.
return (count * 100) / s.length(); // Integer division automatically rounds down.
}
};
JavaScript/TypeScript:
function percentageLetter(s, letter) {
const count = s.split('').filter(c => c === letter).length; //Counts occurrences using filter.
return Math.floor((count * 100) / s.length); //Uses Math.floor for explicit rounding down.
}
All the provided code examples follow the same algorithmic steps, with minor syntactic variations based on the language. They all achieve the same outcome with the specified time and space complexity.