Percentage of Letter in String

EASY

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.

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 letter from s. This can be done using s.replace(String.valueOf(letter), "").
  • The count of letter is 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 letter with an empty string.
  • Calculate the count of letter by 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

Time Complexity: O(N), where N is the length of the string `s`. The `replace()` method needs to scan the entire string to find and replace characters, which takes linear time.Space Complexity: O(N), where N is the length of the string `s`. A new string is created by the `replace()` method. In the worst case (if the letter is not found), the new string has the same length as the original, requiring O(N) space.

Pros and Cons

Pros:
  • Can be written concisely.
  • Leverages built-in string functions, which can be expressive.
Cons:
  • Less efficient in terms of space due to the creation of a new string object.
  • The replace operation 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



Data Structures:

String

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.