Percentage of Letter in String
EASYDescription
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 <= 100sconsists of lowercase English letters.letteris a lowercase English letter.
Approaches
Checkout 2 different approaches to solve Percentage of Letter in String. Click on different approaches to view the approach and algorithm in detail.
String Manipulation with replace()
This approach calculates the count of the target letter by leveraging string manipulation. It determines the number of occurrences by comparing the original string's length with the length of a new string created by removing all instances of the target letter.
Algorithm
- Get the original length of the string
s. - Create a new string by removing all occurrences of
letterfroms. This can be done usings.replace(String.valueOf(letter), ""). - The count of
letteris the difference between the original length and the new string's length. - Calculate the percentage using integer division:
(count * 100) / original_length. This automatically handles the rounding down.
The core idea is to find the difference in length before and after removing the specified letter from the string s. The number of characters removed is equal to the number of occurrences of that letter.
- Store the original length of the string
s. - Create a new string by replacing all occurrences of
letterwith an empty string. - Calculate the count of
letterby subtracting the new string's length from the original length. - Calculate the percentage using the formula:
(count * 100) / original_length. - Since we are performing integer division, the result is automatically floored (rounded down), which matches the problem's requirement.
class Solution {
public int percentageLetter(String s, char letter) {
int originalLength = s.length();
// Create a new string without the target letter
String newString = s.replace(String.valueOf(letter), "");
int newLength = newString.length();
// The count is the difference in lengths
int count = originalLength - newLength;
// Calculate percentage using integer division for flooring
return (count * 100) / originalLength;
}
}
Complexity Analysis
Pros and Cons
- Can be written concisely.
- Leverages built-in string functions, which can be expressive.
- Less efficient in terms of space due to the creation of a new string object.
- The
replaceoperation might have a higher constant time factor than a simple loop.
Code Solutions
Checking out 3 solutions in different languages for Percentage of Letter in String. Click on different languages to view the code.
class Solution {
public
int percentageLetter(String s, char letter) {
int cnt = 0;
for (char c : s.toCharArray()) {
if (c == letter) {
++cnt;
}
}
return cnt * 100 / s.length();
}
}
Video Solution
Watch the video walkthrough for Percentage of Letter in String
Similar Questions
5 related questions you might find useful
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.