Skip to content

2278. Percentage of Letter in String 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
class Solution {
 public:
  int percentageLetter(string s, char letter) {
    return 100 * ranges::count(s, letter) / s.length();
  }
};
1
2
3
4
5
class Solution {
  public int percentageLetter(String s, char letter) {
    return 100 * (int) s.chars().filter(c -> c == letter).count() / s.length();
  }
}
1
2
3
class Solution:
  def percentageLetter(self, s: str, letter: str) -> int:
    return 100 * s.count(letter) // len(s)