EasyStringsAI interview only
Percentage of Letter in String
Asked atamazongooglemicrosoft
01 · Problem
You are given a string s and a single character letter. Return the percentage of characters in s that are equal to letter, rounded down to the nearest whole number.
In other words, compute floor(count * 100 / s.length) where count is the number of positions holding letter. Return the result as an integer between 0 and 100.
02 · Examples
Example 01
Input
s = "foobar", letter = "o"
Output
33
The letter "o" appears 2 times out of 6 characters. 2 / 6 = 33.33...%, which rounds down to 33.
Example 02
Input
s = "aabbb", letter = "b"
Output
60
"b" appears 3 times out of 5 characters, exactly 60%.
Example 03
Input
s = "jjjj", letter = "k"
Output
0
"k" never appears, so the percentage is 0.
03 · Constraints
- 011 <= s.length <= 100
- 02s consists of lowercase English letters
- 03letter is a single lowercase English letter
- 04The answer is an integer in the range [0, 100]
04 · Optimal complexity
- Time
- O(n)
- Space
- O(1)
05 · Two ways to work on it
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.