Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2278.html
2278. Percentage of Letter in String
- Difficulty: Easy.
- Related Topics: String.
- Similar Questions: Sort Characters By Frequency.
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.**
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.
Solution (Java, C++, Python)
-
class Solution { public int percentageLetter(String s, char letter) { int count = 0; int n = s.length(); for (int i = 0; i < n; i++) { if (s.charAt(i) == letter) { ++count; } } return (count * 100) / (n); } }
-
Todo
-
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return s.count(letter) * 100 // len(s) ############ # 2278. Percentage of Letter in String # https://leetcode.com/problems/percentage-of-letter-in-string/ class Solution: def percentageLetter(self, s: str, letter: str) -> int: n = len(s) c = s.count(letter) return int(c / n * 100)
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(n).