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.
This approach involves scanning the string once to count the occurrences of the given letter. Then, calculate the percentage of occurrences by dividing the count of the letter by the total length of the string and multiplying by 100. Since the result needs to be rounded down, use integer division.
We iterate through the string s, counting occurrences of the letter. Calculate the percentage by performing (count * 100) / length and return the result, as C performs integer division by default for integers.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), no additional space other than variables.
Here, we'll use a slightly different approach by calculating the proportion of letter as a floating-point number first. We'll later obtain the percentage by explicitly converting it to an integer, ensuring the result is always correctly rounded down.
Compute the proportion of letter as a floating-point number first. Convert it into an integer percentage using floor to ensure it is rounded down properly.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1).
We can traverse the string s and count the number of characters that are equal to letter. Then, we calculate the percentage using the formula count times 100 \, / \, len(s).
Time complexity is O(n), where n is the length of the string s. Space complexity is O(1).
| Approach | Complexity |
|---|---|
| Linear Scan Approach | Time Complexity: O(n), where n is the length of the string. |
| Count and Proportion Method | Time Complexity: O(n), where n is the length of the string. |
| Counting | — |
| 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. |
Leetcode 2278-Percentage of Letter in String || Easy in தமிழ் || Cpp || Java #dsa #coding #leetcode • code kural • 1,166 views views
Watch 9 more video solutions →Practice Percentage of Letter in String with our built-in code editor and test cases.
Practice on FleetCode