Skip to main content

Percentage of Letter in String - Solution & Explanation

EasyString12 min readAsked at: American Express
Practice this problem

Problem Statement

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.

Approach Overview

Problem Overview: You are given a string s and a character letter. The task is to compute what percentage of characters in s match letter. The result is the integer percentage calculated as (count(letter) * 100) / len(s), using integer division.

Approach 1: Linear Scan Approach (Time: O(n), Space: O(1))

The most direct solution is a single pass through the string. Iterate through every character in s and increment a counter whenever the character equals letter. After the scan finishes, compute the percentage using the formula (count * 100) // n, where n is the string length. This works because you only need the total occurrences of one character, so a full frequency map is unnecessary. The algorithm touches each character once and stores only a small integer counter, giving O(n) time and O(1) space. Problems like this often appear in basic string traversal practice where careful iteration and counting are enough.

Approach 2: Count and Proportion Method (Time: O(n), Space: O(1))

Many languages provide a built-in function to count character occurrences. Instead of manually iterating, call the string's count() or equivalent method to get how many times letter appears in s. Once the count is available, compute the percentage using integer division: (count * 100) // len(s). Internally, the count operation still performs a full scan of the string, so the time complexity remains O(n). The advantage is cleaner code and fewer lines during interviews or coding assessments. This method is common in string manipulation tasks where the language runtime already provides optimized counting utilities.

Both approaches rely on a simple proportional calculation from basic math: convert a raw count into a percentage relative to the total string length. Since no auxiliary data structures are required, the memory footprint stays constant.

Recommended for interviews: The linear scan approach is typically expected. It demonstrates that you can iterate through a string, maintain a counter, and compute a proportion correctly. Mentioning the built-in count method is useful because it shows familiarity with standard library utilities, but implementing the scan yourself makes the underlying logic explicit.

Approach 1: Linear Scan Approach

This approach involves scanning the string once to count the occurrences of the given letter. Then, calculate the percentage of occurrences by dividing the count of the letter by the total length of the string and multiplying by 100. Since the result needs to be rounded down, use integer division.

We iterate through the string s, counting occurrences of the letter. Calculate the percentage by performing (count * 100) / length and return the result, as C performs integer division by default for integers.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), no additional space other than variables.

Try this approach in the editor →

Approach 2: Count and Proportion Method

Here, we'll use a slightly different approach by calculating the proportion of letter as a floating-point number first. We'll later obtain the percentage by explicitly converting it to an integer, ensuring the result is always correctly rounded down.

Compute the proportion of letter as a floating-point number first. Convert it into an integer percentage using floor to ensure it is rounded down properly.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Counting

We can traverse the string s and count the number of characters that are equal to letter. Then, we calculate the percentage using the formula count times 100 \, / \, len(s).

Time complexity is O(n), where n is the length of the string s. Space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Linear Scan Approach

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), no additional space other than variables.

Count and Proportion Method

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1).

Counting

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Linear Scan ApproachO(n)O(1)Standard interview solution when manually iterating through the string.
Count and Proportion MethodO(n)O(1)When the language provides a built-in count function and concise code is preferred.

Video Solution

Percentage of Letter in String | Leetcode 2278 | Easy Peasyt Math | Contest 294 🔥🔥Coding Decoded710 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Percentage of Letter in String easy or hard?
Percentage of Letter in String is considered an Easy problem. It focuses on basic string traversal, counting occurrences, and performing a simple percentage calculation.
Percentage of Letter in String Python/Java solution
In Python, you can use s.count(letter) to get the number of matches and return (count * 100) // len(s). In Java, iterate through the string using a loop, increment a counter when s.charAt(i) equals the target letter, then compute the integer percentage.
How to solve Percentage of Letter in String in O(n)?
Scan the string once and increment a counter whenever the current character equals the given letter. After finishing the iteration, compute the percentage using integer division: (count * 100) // len(s). Because each character is processed exactly once, the algorithm runs in O(n) time.
What is the best approach for Percentage of Letter in String?
The linear scan approach is the most common solution. Iterate through the string once, count how many characters match the given letter, then compute (count * 100) // n where n is the string length. This runs in O(n) time and O(1) space.
Is Percentage of Letter in String asked at Google/Amazon/Meta?
This problem is classified as an easy string traversal problem and appears frequently in coding practice platforms. While large companies typically ask harder variations, the underlying skills—string iteration and counting—are common building blocks in technical interviews.
What data structure is used in Percentage of Letter in String?
No complex data structure is required. The solution only uses the input string and a simple integer counter to track occurrences of the target character. The algorithm relies purely on sequential string traversal.
What is the time complexity of Percentage of Letter in String?
The time complexity is O(n), where n is the length of the string. Every character must be checked once to determine whether it matches the target letter. The space complexity remains O(1) since only a counter variable is stored.

Ready to solve this problem?

Practice Percentage of Letter in String with our built-in code editor and test cases.

Practice on FleetCode