LeetCode Problem Workspace
Percentage of Letter in String
Calculate the percentage of a specific letter in a string using a direct string-driven counting approach, rounding down correctly.
1
Topics
6
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Calculate the percentage of a specific letter in a string using a direct string-driven counting approach, rounding down correctly.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
Start by counting how many times the target letter appears in the string. Divide this count by the total string length and multiply by 100 to get the percentage. Always round down to ensure the output matches the problem requirements exactly.
Problem Statement
You are given a string s and a lowercase character letter. Your task is to return the percentage of characters in s that are equal to letter, rounded down to the nearest whole percent.
For example, if s = "foobar" and letter = "o", the output should be 33 because 2 of 6 characters match 'o'. Handle cases where the letter does not appear at all and ensure all calculations follow integer rounding rules.
Examples
Example 1
Input: s = "foobar", letter = "o"
Output: 33
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
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.
Solution Approach
Count Occurrences of Letter
Iterate through the string s and count each occurrence of the target letter. This is the core string-driven strategy to directly quantify matches without extra data structures.
Calculate Percentage
Divide the letter count by the length of s, multiply by 100, and use floor division or integer cast to round down. This ensures the returned value adheres to the problem's rounding requirement.
Return Result
Return the computed integer percentage. Edge cases include when the letter is absent (return 0) or when the entire string matches the letter (return 100).
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) where n is the length of s because each character is inspected once. Space complexity is O(1) since only counters are used, independent of input size.
What Interviewers Usually Probe
- Can you count how many times the letter appears in s efficiently?
- Do you handle rounding down correctly for percentages less than 1?
- How do you handle the case when the letter does not appear at all?
Common Pitfalls or Variants
Common pitfalls
- Forgetting to round down and returning a float or rounded value incorrectly.
- Miscounting letter occurrences in strings with repeated characters.
- Not handling empty or single-character strings according to constraints.
Follow-up variants
- Return the percentage as a floating-point number instead of rounding down.
- Compute the percentage of multiple letters at once in the same string.
- Handle uppercase letters and case-insensitive matching.
FAQ
What is the fastest way to count letter occurrences in a string?
Iterate through the string once, incrementing a counter when a character matches the target letter. This O(n) approach is simple and direct.
How do I ensure the percentage is rounded down?
Use integer division or a floor function after multiplying by 100 to round down automatically.
What if the letter does not appear in the string?
Return 0 because no characters match the target letter, following the problem requirement for rounding.
Can this solution handle strings of length 1?
Yes, the same counting and percentage calculation works, returning either 0 or 100 as appropriate.
Is this problem pattern considered a string-driven solution strategy?
Yes, it relies entirely on scanning the string and counting target characters, exemplifying the string-driven pattern.
Solution
Solution 1: Counting
We can traverse the string $\textit{s}$ and count the number of characters that are equal to $\textit{letter}$. Then, we calculate the percentage using the formula $\textit{count} \times 100 \, / \, \textit{len}(\textit{s})$.
class Solution:
def percentageLetter(self, s: str, letter: str) -> int:
return s.count(letter) * 100 // len(s)Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward