2278. Percentage of Letter in String

Easy

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.

解題

這題如果用 float32 , 會在一題測資出錯。

Runtime: 0 ms, faster than 100.00%

Memory Usage: 2 MB, less than 61.82%

func percentageLetter(s string, letter byte) int {
    count := 0
    
    for i:=0; i<len(s); i++ {
        if s[i] == letter { count++ }
    }
    
    return int((float64(count) / float64(len(s))) * 100)
}

Last updated