Percentage of Letter in String solution and explanation
9 more videos on this problem
Percentage of Letter in String | Leetcode 2278 | Easy Peasyt Math | Contest 294 🔥🔥Coding Decoded · 2:52 · 710 views
Leetcode | 2278. Percentage of Letter in String | Easy | Java SolutionDeveloper Docs · 4:54 · 473 views
2278 Percentage of Letter in String | Zero to FAANG | Weekly 294 | Leetcode | Striver | WE2Programmers Zone · 6:48 · 408 views
#10. LeetCode | Percentage of Letter in String | Java 11 | Easy | #leetcodeAmod Mahajan · 5:34 · 383 views
LeetCode 2278 – Percentage of Letter in String | Fast Java Solution in 5 MinutesRecurso · 6:33 · 351 views
LeetCode Weekly Contest 294 | Percentage of Letter in String | PythonBayu Aji · 1:57 · 309 views
Leetcode Weekly Contest 294 | PROBLEM 2278 , 2279 , 2280 | MATHS | GEOMETRYcode Explainer · 17:22 · 303 views
2278. Percentage of Letter in String with Python | Leetcode with Python #leetcode #youtube #python3Arsalan Ahmed · 5:04 · 264 views
2278. Percentage of Letter in String (Leetcode Easy)Programming Live with Larry · 2:09 · 246 views
Leetcode 2278. Percentage of Letter in String | EasyThinkCode · 0:42 · 234 views
The problem
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.
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.
def percentage_letter(s, letter):
count = sum(1 for char in s if char == letter)
return (count * 100) // len(s)
print(percentage_letter('foobar', 'o')) # Output: 33
print(percentage_letter('jjjj', 'k')) # Output: 0public class Main {
public static int percentageLetter(String s, char letter) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == letter) count++;
}
return (count * 100) / s.length();
}
public static void main(String[] args) {
System.out.println(percentageLetter("foobar", 'o')); // Output: 33
System.out.println(percentageLetter("jjjj", 'k')); // Output: 0
}
}#include <iostream>
#include <string>
using namespace std;
int percentage_letter(const string& s, char letter) {
int count = 0;
for (char c : s) {
if (c == letter) count++;
}
return (count * 100) / s.length();
}
int main() {
cout << percentage_letter("foobar", 'o') << endl; // Output: 33
cout << percentage_letter("jjjj", 'k') << endl; // Output: 0
return 0;
}function percentageLetter(s, letter) {
let count = 0;
for (let char of s) {
if (char === letter) count++;
}
return Math.floor((count * 100) / s.length);
}
console.log(percentageLetter("foobar", 'o')); // Output: 33
console.log(percentageLetter("jjjj", 'k')); // Output: 0#include <stdio.h>
#include <string.h>
int percentage_letter(char* s, char letter) {
int count = 0, length = strlen(s);
for (int i = 0; i < length; ++i) {
if (s[i] == letter) count++;
}
return (count * 100) / length;
}
int main() {
printf("%d\n", percentage_letter("foobar", 'o')); // Output: 33
printf("%d\n", percentage_letter("jjjj", 'k')); // Output: 0
return 0;
}using System;
public class Program
{
public static int PercentageLetter(string s, char letter)
{
int count = 0;
foreach (char c in s)
{
if (c == letter) count++;
}
return (count * 100) / s.Length;
}
public static void Main()
{
Console.WriteLine(PercentageLetter("foobar", 'o')); // Output: 33
Console.WriteLine(PercentageLetter("jjjj", 'k')); // Output: 0
}
}We use a generator expression to sum the occurrences of letter in s, then calculate the integer percentage using floor division.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1).
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.
def percentage_letter(s, letter):
count = sum(1 for char in s if char == letter)
return int((count * 100.0) // len(s))
print(percentage_letter('foobar', 'o')) # Output: 33
print(percentage_letter('jjjj', 'k')) # Output: 0import java.lang.Math;
public class Main {
public static int percentageLetter(String s, char letter) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == letter) count++;
}
return (int)Math.floor((count * 100.0) / s.length());
}
public static void main(String[] args) {
System.out.println(percentageLetter("foobar", 'o')); // Output: 33
System.out.println(percentageLetter("jjjj", 'k')); // Output: 0
}
}#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int percentageLetter(const string& s, char letter) {
int count = 0;
for (char c : s) {
if (c == letter) count++;
}
return static_cast<int>(floor((count * 100.0) / s.length()));
}
int main() {
cout << percentageLetter("foobar", 'o') << endl; // Output: 33
cout << percentageLetter("jjjj", 'k') << endl; // Output: 0
return 0;
}function percentageLetter(s, letter) {
let count = 0;
for (let char of s) {
if (char === letter) count++;
}
return Math.floor((count * 100.0) / s.length);
}
console.log(percentageLetter("foobar", 'o')); // Output: 33
console.log(percentageLetter("jjjj", 'k')); // Output: 0#include <stdio.h>
#include <string.h>
#include <math.h>
int percentageLetter(char* s, char letter) {
int count = 0, length = strlen(s);
for (int i = 0; i < length; ++i) {
if (s[i] == letter) count++;
}
return (int)floor((count * 100.0) / length);
}
int main() {
printf("%d\n", percentageLetter("foobar", 'o')); // Output: 33
printf("%d\n", percentageLetter("jjjj", 'k')); // Output: 0
return 0;
}using System;
public class Program
{
public static int PercentageLetter(string s, char letter)
{
int count = 0;
foreach (char c in s)
{
if (c == letter) count++;
}
return (int)Math.Floor((count * 100.0) / s.Length);
}
public static void Main()
{
Console.WriteLine(PercentageLetter("foobar", 'o')); // Output: 33
Console.WriteLine(PercentageLetter("jjjj", 'k')); // Output: 0
}
}Using // in Python both truncates the division and avoids the need for explicit flooring, though casting it to an int reinforces the conversion into an integer.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1).
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).
function percentageLetter(s: string, letter: string): number {
const count = s.split('').filter(c => c === letter).length;
return Math.floor((100 * count) / s.length);
}func percentageLetter(s string, letter byte) int {
return strings.Count(s, string(letter)) * 100 / len(s)
}impl Solution {
pub fn percentage_letter(s: String, letter: char) -> i32 {
let count = s.chars().filter(|&c| c == letter).count();
(100 * count as i32 / s.len() as i32) as i32
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Linear Scan Approach | Time Complexity: O(n), where n is the length of the string. |
| Count and Proportion Method | Time Complexity: O(n), where n is the length of the string. |
| Counting | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Linear Scan Approach | O(n) | O(1) | Standard interview solution when manually iterating through the string. |
| Count and Proportion Method | O(n) | O(1) | When the language provides a built-in count function and concise code is preferred. |
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.
