Leetcode 2278. Percentage of Letter in String

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Percentage of Letter in String

2. Solution

解析:Version 1,先统计出字符个数,然后求百分比即可。

  • Version 1
1
2
3
4
5
6
7
8
9
class Solution:
def percentageLetter(self, s: str, letter: str) -> int:
n = len(s)
count = 0
for ch in s:
if ch == letter:
count += 1
percent = count * 100 // n
return percent

Reference

  1. https://leetcode.com/problems/percentage-of-letter-in-string/
如果有收获,可以请我喝杯咖啡!