Largest 3-Same-Digit Number in String (LeetCode 2264): Solution and Explanation

Easy
String

Find the largest substring of length 3 made of one repeated digit (like 777 or 000) inside a digit string num, or return "" if there is none.

Intuition

Every candidate answer has the shape ddd for one digit d, and it must appear as three neighbouring characters. So the only places worth looking at are the n - 2 windows of length 3.

A window is good when its three digits are equal. Checking that takes two comparisons, so scanning all windows is linear.

All candidates have the same length, so comparing them as strings orders them exactly like the numbers they spell. "000" is the smallest good integer but still beats the empty string, which is why the answer for 2300019 is "000" and not "".

Approach: Sliding window of length 3

  1. Set best = "".
  2. For each start i from 0 to n - 3, check whether num[i], num[i + 1] and num[i + 2] are equal.
  3. If they are, replace best with num[i] * 3 when that is larger.
  4. Return best.

Step-by-step example

num = "6777133339"
  1. Window 677 at i = 0 has different digits. Skip.
  2. Window 777 at i = 1 is good, and best is empty, so best = 777.
  3. Windows 771, 713, 133 are not good.
  4. Window 333 at i = 5 is good, but 333 < 777, so best stays. The window 333 at i = 6 is the same story.
  5. The last window 339 is not good. The answer is "777".

Code: Python, Java, C++

1class Solution:
2    def largestGoodInteger(self, num: str) -> str:
3        best = ''
4        for i in range(len(num) - 2):
5            if num[i] == num[i + 1] == num[i + 2]:
6                best = max(best, num[i] * 3)
7        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

There are n - 2 windows and each is checked with two comparisons.

Space

O(1)

Only the best candidate (at most three characters) is stored.

Other approaches

Check the ten candidates from the top (time O(n), space O(1))

Test "999", "888", down to "000" with a substring search and return the first one found. It is short and also linear, since there are only ten candidates.

Run-length scan (time O(n), space O(1))

Track the length of the current run of equal digits; whenever it reaches 3, update the best digit. Same complexity, a different way to spot the triples.

Common mistakes

  • Converting candidates to integers: "000" becomes 0, which is easy to confuse with 'not found' and loses the leading zeros the answer needs.
  • Looping i up to n - 1 and reading past the end of the string at i + 2.
  • Returning "0" or null instead of the empty string when nothing is found.

Frequently asked questions

Why is string comparison safe here?

All candidates are exactly three characters long, and for equal-length digit strings lexicographic order matches numeric order.

Does a run of four equal digits count?

Yes. A run like 5555 contains the window 555, which is a good integer.

Can I stop early?

Once you find "999", nothing can beat it, so you can return immediately. It does not change the worst-case complexity.

What is the time and space complexity?

O(n) time and O(1) extra space.