Percentage of Letter in String in Java Script

Given a string s and a character letter, return the percentage of characters in s that equal letter rounded down to the nearest whole per cent.

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.
/**
 * @param {string} s
 * @param {character} letter
 * @return {number}
 */
function percentageLetter(s, letter) {
    let count = 0;
    for (let i = 0; i < s.length; i++) {
        if (s[i] === letter) {
            count++;
        }
    }
    return Math.floor((count / s.length) * 100);
}

console.log(percentageLetter("hello world", "l")); 
Percentage of letter in string in javascript array
How to calculate percentage of letter in string in javascript
how to add percentage symbol in javascript
a programmer string contains letters that can be rearranged
sort a string according to the frequency of characters java
sort a string according to the frequency of characters in python
printing frequency of each character just after its consecutive occurrences leetcode
sort string of characters

Same Tree Leet Code Solution in C++

Note: It is just an example you can solve your own question.

Leave a Comment