Talentd
FleetCode

2278. Percentage of Letter in String

Easy75.1% acceptance1 company

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 <= 100
  • s consists of lowercase English letters.
  • letter is a lowercase English letter.

Asked at 1 company

Topics

Hints open one at a time

Hint 1
Can we count the number of occurrences of letter in s?
Hint 2
Recall that the percentage is calculated as (occurrences / total) * 100.
2:52
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def percentageLetter(self, s: str, letter: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"foobar"
"o"
Input
"jjjj"
"k"
Your code is saved on this device.Solution