Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.class Solution {
public:
int percentageLetter(string s, char letter) {
int count=0;
for(int i=0;i<s.length();i++)
{ if(s[i]==letter)
{
count++;
}
}
return (count*100)/s.length();
}
};
class Solution:
def percentageLetter(self, s: str, letter: str) -> int:
return (s.count(letter)*100)//len(s)
class Solution {
public int percentageLetter(String s, char letter) {
int count=0; //count acts as a counter variabe to count the give character "letter" in the string
//Traverse the string s and each time the character int the string is equal to "letter"
//increment the count variable by 1.
for(char ch:s.toCharArray())
{
if(ch==letter)
count++;
}
//If the count is zero, then the percentage is also zero
if(count==0)
return 0;
// to find the total characters in the strings or we can say to find the length of the string
int total=s.length();
// Calculate the total percentage using the standard percentage formula i.e (given*100)/total
int per=(count*100)/total;
return per;
}
}
In our experience, we suggest you solve this Percentage of Letter in String LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Percentage of Letter in String LeetCode Solution
I hope this Percentage of Letter in String LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>