Ticker

6/recent/ticker-posts

leetcode - 2278 Percentage of the letter in string


In this problem there give one string and one char 

let's take string as "s" and char as "l"


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.

This program calculates the percentage of a specific character, `l`, in a given string `s` and returns it as an integer, rounded down to the nearest whole number. The goal is to determine how many times the character `l` appears in the string, compute its percentage relative to the total length of the string, and return the result. The program begins by determining the length of the string `s` using `s.size()` and initializes a counter variable, `count`, to zero. This variable keeps track of how many times the character `l` appears in the string. A `for` loop is used to iterate through each character of the string `s`. Within the loop, it checks if the current character matches `l`. If a match is found, the counter is incremented. After completing the loop, the program calculates the percentage by dividing `count` by the total length of the string `s`, multiplying the result by 100 to convert it to a percentage. The result is returned as an integer, which automatically rounds down to the nearest whole number due to implicit type conversion from floating-point to integer.


For example, if the input string is `s = "foobar"` and the character is `l = 'o'`, the program finds that `'o'` appears 2 times in a string of length 6. It calculates the percentage as \((2 / 6) \times 100 = 33.33\), which is rounded down to `33`, and the program returns this value. For another example, if the input string is `s = "jjjj"` and the character is `l = 'k'`, since `'k'` does not appear in the string, the count remains zero. The program calculates the percentage as \((0 / 4) \times 100 = 0\) and returns `0`. 


This implementation is efficient, with a time complexity of \(O(n)\), where \(n\) is the length of the string, as it processes each character exactly once. The space complexity is \(O(1)\) since no additional data structures are used. The program handles various edge cases effectively, such as when the character `l` does not appear in the string at all, when the string consists entirely of the character `l`, or when the string is very large. Furthermore, integer division is utilized to automatically round down the result, simplifying the computation. Overall, the program is concise, efficient, and robust for handling inputs of different sizes and configurations.

CODE:

class Solution {
public:
    int percentageLetter(string s, char l) {
        int n=s.size();
        int i;
       double  max=0;
        double  count=0;
        for(i=0;i<n;i++){
            if(s[i]==l){
                count++;
                }
            }
        max+=((count/n)*100);
       
        return max;
           
       
       
    }
};

Also Read


Post a Comment

0 Comments