Skip to content

Latest commit

History

History
53 lines (41 loc) 路 1.32 KB

2278. Percentage of Letter in String.md

File metadata and controls

53 lines (41 loc) 路 1.32 KB

1. Description

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.

2. Solutions

Solution 1: Language: C

  • Monday, 30 May, 2022
  • Time Complexity: $O(n)$
  • Space Complexity: $O(1)$
  • Runtime: 0 ms, faster than 100.00% of C online submissions for Percentage of Letter in String.
  • Memory Usage: 5.4 MB, less than 100.00% of C online submissions for Percentage of Letter in String.
int percentageLetter(char *s, char letter) {
    float counter = 0;
    int i = 0;
    while (s[i] != '\0') {
        if (s[i] == letter) {
            counter += 1;
        }
        i++;
    }
    return (100 * counter) / i;
}