2278 字母在字符串中的百分比

·   ·   ·   ·

  ·   ·


题目链接

2278. 字母在字符串中的百分比

分析

遍历后计算即可,也可以使用STL实现

代码实现

class Solution {
public:
    int percentageLetter(string s, char letter) {
        int cnt = ranges::count(s, letter) * 100;
        return cnt ? floor(1. * cnt / s.size()) : 0;
    }
};

复杂度分析

  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(1)$