Python 2278.py
class Solution:
def percentageLetter(self, s: str, letter: str) -> int:
return 100 * s.count(letter) // len(s)
Difficulty
and has an acceptance rate of about 75% (100k+ submissions).
Typical patterns to solve this problem:
- String
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a single-method class Solution that computes the percentage of a specific character within a given string. The method percentageLetter receives two parameters: the source string s and the target character letter. It returns an integer representing the proportion of occurrences of letter in s, scaled to a whole-number percentage.
Core Logic
The method consists of a single expression that combines three built-in operations:
return 100 * s.count(letter) // len(s)
s.count(letter)traverses the string once to tally how many timesletterappears.len(s)obtains the total number of characters in the string.- Multiplication by
100scales the ratio to a percentage. - The floor division operator
//yields an integer result, discarding any fractional part.
Edge Cases Addressed
The algorithm naturally accommodates several boundary conditions:
- Letter absent from the string -
s.count(letter)returns0, so the final result is0%. - Letter present in every position -
s.count(letter)equalslen(s), producing100%. - String containing a single character - both
s.count(letter)andlen(s)evaluate to1when the character matches, again yielding100%. - Mixed case sensitivity - the method treats characters exactly as they appear; a lower-case
adiffers from an upper-caseA, so the count reflects the exact match. - Non-alphabetic characters - any Unicode or ASCII symbol can be supplied as
letter; the count operation works uniformly across character types.
Performance Optimizations
The implementation leverages Python’s native string methods, which are executed in highly optimized C code. This yields several performance benefits:
- Single pass counting -
s.count(letter)scans the string only once, avoiding multiple traversals. - Avoidance of floating-point arithmetic - by scaling before division and using integer floor division, the calculation stays within integer arithmetic, which is faster and eliminates rounding concerns.
- In-place length retrieval -
len(s)is a constant-time operation that does not require iteration over the string’s contents.
Overall, the method delivers a concise, efficient solution for computing character percentages in a string.
ELI5 - Simplified Explanation
Imagine a long train made of many colorful blocks. Each block has a letter written on it, and the whole train is named s. Sometimes you want to figure out how much of the train is made of a specific color, like red.
First, the code looks at the whole train and counts every single block that has the special letter on it. Next, it figures out how many blocks are in the entire train altogether. By dividing the number of special blocks by the total number of blocks, it calculates a small portion. Finally, it multiples that portion by one hundred to turn it into a percentage, which tells you exactly how many of those blocks are the specific letter out of every hundred blocks.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | |||||||||
| www | AlgoMonster | Java | Python | C++ | TS | |||||
| WalkCCC | Java | Python | C++ | |||||||
| leetcode.ca | Java | Python | Go | C++ | Rust | TS | ||||
| Progiez | Java | Python | C++ | |||||||
| github | chandansgowda | Python | ||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| JoshCrozier | JS | |||||||||
| Krazune | Java | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| varunu28 | Java | |||||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||||
| WalkCCC | C++, Python, Java | |||||||||
| leetcode.ca | C++, Go, Java, Python, Rust, TypeScript | |||||||||
| Progiez | C++, Java, Python | |||||||||
| chandansgowda | Python | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| JoshCrozier | JavaScript | |||||||||
| Krazune | Java | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| varunu28 | Java |
Videos
Similar Problems
- problem 451: Sort Characters By Frequency
Solved On The Same Day
- problem 2273: Find Resultant Array After Removing Anagrams
- problem 2270: Number of Ways to Split Array
- problem 2274: Maximum Consecutive Floors Without Special Floors
- problem 2966: Divide Array Into Arrays With Max Difference
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality