Largest 3-Same-Digit Number in String (LeetCode 2264): Solution and Explanation
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
- Set
best = "". - For each start
ifrom0ton - 3, check whethernum[i],num[i + 1]andnum[i + 2]are equal. - If they are, replace
bestwithnum[i] * 3when that is larger. - Return
best.
Step-by-step example
num = "6777133339"
- Window
677at i = 0 has different digits. Skip. - Window
777at i = 1 is good, and best is empty, so best =777. - Windows
771,713,133are not good. - Window
333at i = 5 is good, but333<777, so best stays. The window333at i = 6 is the same story. - The last window
339is 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 best1class Solution {
2 public String largestGoodInteger(String num) {
3 String best = "";
4 for (int i = 0; i + 2 < num.length(); i++) {
5 char c = num.charAt(i);
6 if (c == num.charAt(i + 1) && c == num.charAt(i + 2)) {
7 String cand = num.substring(i, i + 3);
8 if (cand.compareTo(best) > 0) {
9 best = cand;
10 }
11 }
12 }
13 return best;
14 }
15}1class Solution {
2public:
3 string largestGoodInteger(string num) {
4 string best = "";
5 for (size_t i = 0; i + 2 < num.size(); i++) {
6 char c = num[i];
7 if (c == num[i + 1] && c == num[i + 2]) {
8 best = max(best, num.substr(i, 3));
9 }
10 }
11 return best;
12 }
13};Time and space complexity
O(n)
There are n - 2 windows and each is checked with two comparisons.
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
iup ton - 1and reading past the end of the string ati + 2. - Returning
"0"ornullinstead 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.