Percentage of Letter in String - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: s = "foobar", letter = "o"
Output: 33
💡 Note: There are 2 'o's in "foobar" out of 6 characters. Percentage = (2/6) * 100 = 33.33, floored to 33.
Example 2 — All Same Character
$ Input: s = "jjjj", letter = "k"
Output: 0
💡 Note: There are 0 'k's in "jjjj". Percentage = (0/4) * 100 = 0.
Example 3 — Perfect Match
$ Input: s = "jjjj", letter = "j"
Output: 100
💡 Note: All 4 characters are 'j'. Percentage = (4/4) * 100 = 100.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters
  • letter is a lowercase English letter

Visualization

Tap to expand
Percentage Calculation OverviewInputs = "foobar"letter = "o"ProcessCount: 2 'o'sLength: 6Output(2/6) × 100 = 33.33Floor(33.33) = 33Formula: ⌊(count ÷ length) × 100⌋Result: 33
Understanding the Visualization
1
Input
String and target character
2
Count
Count occurrences of target character
3
Calculate
Apply percentage formula and floor result
Key Takeaway
🎯 Key Insight: Simple counting with percentage math - floor division gives us the rounded down result directly
Asked in
Google 15 Meta 12
12.5K Views
Medium Frequency
~8 min Avg. Time
435 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen