Python 2264.py
class Solution:
def largestGoodInteger(self, num: str) -> str:
for i in range(9, -1, -1):
iii = str(i) * 3
if num.count(iii) > 0:
return iii
return ""
Difficulty
and has an acceptance rate of about 73% (400k+ 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 class Solution with a single method largestGoodInteger. The method receives a numeric string num and searches for the largest three-digit substring that consists of the same digit repeated three times (for example, "777"). If such a substring exists, the method returns it; otherwise it returns an empty string. The algorithm proceeds by examining possible triples in descending order, guaranteeing that the first match found is the maximal one.
Key Steps
The core logic is a descending loop over the digits 9 through 0. For each digit the code constructs a three-character string composed of that digit repeated three times and checks whether this pattern appears anywhere in the input string.
for i in range(9, -1, -1):
iii = str(i) * 3
if num.count(iii) > 0:
return iii
If the loop finishes without finding any matching triple, the method falls back to returning an empty string.
return ""
The method is encapsulated within a class to match typical LeetCode or similar platform conventions.
class Solution:
def largestGoodInteger(self, num: str) -> str:
# loop and check logic here
Edge Cases Handled
When the input string is shorter than three characters, the count operation will never find a triple, and the method correctly returns an empty string. An empty input string follows the same path, yielding an empty result.
If the input contains multiple qualifying triples, the descending order of the loop ensures that the highest numeric triple is selected. For instance, given "555777111", the loop will encounter "777" before "555" and return "777".
When the input consists entirely of a single repeated digit, such as "2222", the count call will detect the triple "222" and return it immediately.
If the input contains overlapping triples, for example "1111" (which contains "111" starting at positions 0 and 1), the count method still reports a positive count, leading to a correct return of "111".
Performance Optimizations
The algorithm limits its search space to a fixed set of ten candidate triples, independent of the length of the input string. By iterating from the largest digit downward, it can terminate as soon as the first match is found, avoiding unnecessary checks for smaller digits. This early-exit strategy reduces the total number of scans over the input string in typical cases where a high-value triple exists early in the search order.
The use of Python’s built-in str.count leverages a highly optimized C implementation for substring counting, providing fast execution for each candidate triple. Because the loop runs a constant number of times (ten iterations at most), the overall runtime remains bounded and efficient for any permissible input size.
ELI5 - Simplified Explanation
Imagine a big long number made of many digits standing in a line like train cars. The goal is to find the biggest set of three identical digits sitting right next to each other. To find the answer, this computer program starts by looking for three nines in a row. If it finds the three nines, it stops and says that is the winner because nine is the largest number. If it does not find any nines, it moves on to look for three eights in a row instead. The program keeps doing this, checking each number one by one all the way down to three zeros, just to see if it can spot a matching triplet. The very first time it finds a matching set of three, it knows it has found the biggest one possible.
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 | C++ | Go | TS | |||
| Progiez | Java | Python | C++ | |||||
| github | charles-wangkai | Java | ||||||
| everthis | JS | |||||||
| fartem | Ruby | |||||||
| fsq | C++ | |||||||
| JoshCrozier | JS | |||||||
| lkwq007 | Python | |||||||
| varunu28 | Java | |||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||
| WalkCCC | C++, Python, Java | |||||||
| leetcode.ca | C++, Go, Java, Python, TypeScript | |||||||
| Progiez | C++, Java, Python | |||||||
| charles-wangkai | Java | |||||||
| everthis | JavaScript | |||||||
| fartem | Ruby | |||||||
| fsq | C++ | |||||||
| JoshCrozier | JavaScript | |||||||
| lkwq007 | Python | |||||||
| varunu28 | Java |
Videos
Similar Problems
- problem 1903: Largest Odd Number in String
Solved On The Same Day
- problem 953: Verifying an Alien Dictionary
- problem 961: N-Repeated Element in Size 2N Array
- problem 2682: Find the Losers of the Circular Game
- problem 2784: Check if Array is Good
- problem 2848: Points That Intersect With Cars
- problem 2869: Minimum Operations to Collect Elements
- problem 890: Find and Replace Pattern
- problem 954: Array of Doubled Pairs
- problem 959: Regions Cut By Slashes
- problem 966: Vowel Spellchecker
- problem 1202: Smallest String With Swaps
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