Problem 2278

Abstract / Summary

Find what percent of the letters in a string are a specific given letter.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 2278.py

class Solution:
    def percentageLetter(self, s: str, letter: str) -> int:
        return 100 * s.count(letter) // len(s)
3 lines | 126 bytes | submitted February 2, 2024 | GitHub   tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 75% (100k+ submissions).

Typical patterns to solve this problem:
  • String
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
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.
LeetCode profile | GitHub repo

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 times letter appears.
  • len(s) obtains the total number of characters in the string.
  • Multiplication by 100 scales 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) returns 0, so the final result is 0%.
  • Letter present in every position - s.count(letter) equals len(s), producing 100%.
  • String containing a single character - both s.count(letter) and len(s) evaluate to 1 when the character matches, again yielding 100%.
  • Mixed case sensitivity - the method treats characters exactly as they appear; a lower-case a differs from an upper-case A, 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.

written by openai gpt

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.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 8 | μ = 7.78 | σ = 1.79

Complexity Analysis

Time: linear
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
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
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
26 links | 14 authors | 9 languages

Videos

7 videos   did my crawler come up with invalid videos ? drop me a line.

Similar Problems

1 similar problem

Solved On The Same Day

5 solutions   February 2, 2024

Extra

There are a few extra pages giving you some insights into my setup:
  • 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