Problem 2264

Abstract / Summary

Find the largest three digit number with all same digits that can be formed from the digits in the given string.

Note

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

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 ""
7 lines | 218 bytes | submitted December 4, 2023 | GitHub   tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 73% (400k+ 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 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.

written by openai gpt

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.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 26 | median = 7 | μ = 7.19 | σ = 0.96

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 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
22 links | 11 authors | 7 languages

Videos

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

Similar Problems

1 similar problem

Solved On The Same Day

12 solutions   December 4, 2023

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