Watch 10 video solutions for Percentage of Letter in String, a easy level problem involving String. This walkthrough by code kural has 1,166 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 100s consists of lowercase English letters.letter is a lowercase English letter.Problem Overview: You are given a string s and a character letter. The task is to compute what percentage of characters in s match letter. The result is the integer percentage calculated as (count(letter) * 100) / len(s), using integer division.
Approach 1: Linear Scan Approach (Time: O(n), Space: O(1))
The most direct solution is a single pass through the string. Iterate through every character in s and increment a counter whenever the character equals letter. After the scan finishes, compute the percentage using the formula (count * 100) // n, where n is the string length. This works because you only need the total occurrences of one character, so a full frequency map is unnecessary. The algorithm touches each character once and stores only a small integer counter, giving O(n) time and O(1) space. Problems like this often appear in basic string traversal practice where careful iteration and counting are enough.
Approach 2: Count and Proportion Method (Time: O(n), Space: O(1))
Many languages provide a built-in function to count character occurrences. Instead of manually iterating, call the string's count() or equivalent method to get how many times letter appears in s. Once the count is available, compute the percentage using integer division: (count * 100) // len(s). Internally, the count operation still performs a full scan of the string, so the time complexity remains O(n). The advantage is cleaner code and fewer lines during interviews or coding assessments. This method is common in string manipulation tasks where the language runtime already provides optimized counting utilities.
Both approaches rely on a simple proportional calculation from basic math: convert a raw count into a percentage relative to the total string length. Since no auxiliary data structures are required, the memory footprint stays constant.
Recommended for interviews: The linear scan approach is typically expected. It demonstrates that you can iterate through a string, maintain a counter, and compute a proportion correctly. Mentioning the built-in count method is useful because it shows familiarity with standard library utilities, but implementing the scan yourself makes the underlying logic explicit.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Linear Scan Approach | O(n) | O(1) | Standard interview solution when manually iterating through the string. |
| Count and Proportion Method | O(n) | O(1) | When the language provides a built-in count function and concise code is preferred. |