[LeetCode] Percentage of Letter in String

2278. Percentage of Letter in String

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int percentageLetter(string s, char letter) {
int count = 0;
for(auto& ch : s)
if(ch == letter) count++;
double res = 100.0 * count / s.length();
return floor(res);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/22/PS/LeetCode/percentage-of-letter-in-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.