2264. Largest 3-Same-Digit Number in String - Explanation

Problem Link

Description

You are given a string num representing a large integer. An integer is good if it meets the following conditions:

  • It is a substring of num with length 3.
  • It consists of only one unique digit.

Return the maximum good integer as a string or an empty string "" if no such integer exists.

Note:

  • A substring is a contiguous sequence of characters within a string.
  • There may be leading zeroes in num or a good integer.

Example 1:

Input: num = "6777133339"

Output: "777"

Explanation: There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".

Example 2:

Input: num = "2300019"

Output: "000"

Explanation: "000" is the only good integer.

Example 3:

Input: num = "42352338"

Output: ""

Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.

Constraints:

  • 3 <= num.length <= 1000
  • num only consists of digits.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.



Prerequisites

Before attempting this problem, you should be comfortable with:

  • String Traversal - Iterating through characters in a string and accessing elements by index
  • Sliding Window (Fixed Size) - Examining consecutive elements of a fixed length (in this case, 3 characters)
  • String Comparison - Comparing strings lexicographically or converting substrings to integers for comparison

1. Brute Force

Intuition

We need to find the largest "good integer" in the string, where a good integer is a substring of length 3 consisting of the same digit repeated three times (like "111", "222", etc.). The straightforward approach is to scan through the string, check every window of size 3, and whenever we find three consecutive identical digits, we compare its numeric value to our current best and keep the larger one.

Algorithm

  1. Initialize res as an empty string and val as 0 to track the largest good integer found.
  2. Iterate through the string from index 0 to len(num) - 2:
    • Check if the current character equals the next two characters.
    • If so, extract the 3-character substring and convert it to an integer.
    • If this value is greater than or equal to val, update both val and res.
  3. Return res.
class Solution:
    def largestGoodInteger(self, num: str) -> str:
        res = ""
        val = 0

        for i in range(len(num) - 2):
            if num[i] == num[i + 1] == num[i + 2]:
                tmp = num[i : i + 3]
                if val <= int(tmp):
                    val = int(tmp)
                    res = tmp

        return res

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

2. Iteration

Intuition

Instead of tracking both the numeric value and the string separately, we can simplify by using string comparison. Since all good integers have the same length (3 characters), lexicographic comparison works correctly for finding the maximum. For example, "999" > "888" > "777" when compared as strings. We just need to handle the edge case where "000" is a valid result but we need to distinguish it from "no good integer found."

Algorithm

  1. Initialize res to "0" as a baseline for comparison.
  2. Iterate through the string, checking each window of size 3:
    • If three consecutive characters are the same, compare the substring with res and keep the larger one.
  3. If res is still "0" and "000" was never found, return an empty string. Otherwise, return res.
class Solution:
    def largestGoodInteger(self, num: str) -> str:
        res = "0"

        for i in range(len(num) - 2):
            if num[i] == num[i + 1] == num[i + 2]:
                res = max(res, num[i : i + 3])

        return "" if res == "0" else res

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

3. Iteration (Optimal)

Intuition

We can optimize further by observing that we only need to track the digit itself, not the entire 3-character substring. Since all good integers are formed by repeating a single digit three times, we just need to find the largest digit that appears three times consecutively. At the end, we can construct the result by repeating that digit three times.

Algorithm

  1. Initialize res to -1 to indicate no good integer found yet.
  2. Iterate through the string, checking each window of size 3:
    • If three consecutive characters are the same, extract the digit value and update res if it is larger.
  3. If res is still -1, return an empty string. Otherwise, return the digit repeated three times.
class Solution:
    def largestGoodInteger(self, num: str) -> str:
        res = -1

        for i in range(len(num) - 2):
            if num[i] == num[i + 1] == num[i + 2]:
                res = max(res, int(num[i]))
        return str(res) * 3 if res != -1 else ""

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

Common Pitfalls

Forgetting That "000" Is a Valid Good Integer

The problem asks for the largest good integer, but "000" is still a valid answer if it is the only one found. Initializing the result to an empty string and checking only for non-zero matches causes "000" to be incorrectly skipped. Handle "000" explicitly or use comparison logic that includes it.

Returning the Wrong Result When No Good Integer Exists

If no three consecutive identical digits exist in the string, the answer should be an empty string. Returning "0", a default numeric value, or the last partial match leads to wrong answers. Always track whether any valid match was found before returning.