NeetCode #71LC-2264EasyArrays & Hashing
← Back to All Problems

#71 · #2264 · Largest 3-Same-Digit Number in String(字符串中最大的 3 位相同数字)

📌 Problem Statement & Constraints

You are given a string num representing a large integer. Return the largest three-digit substring of num that consists of the same digit repeated three times, or the empty string if none exists. Constraints: 3 <= num.length <= 1000; num consists of digits.

💡 Core Algorithmic Approaches

  1. Scan all windows of length 3 and check whether all three characters are equal.
  2. Track the maximum such substring as a string, since the digits are already in lexicographic order matching numeric order.
  3. Return the empty string if no window qualifies.
  4. The whole problem is O(n) and needs no conversion to integers -- a useful observation since num can be 1000 digits long.

💻 Benchmark Python3 Implementation

class Solution:
    def largestGoodInteger(self, num: str) -> str:
        best = ""
        for i in range(len(num) - 2):
            if num[i] == num[i + 1] == num[i + 2]:
                best = max(best, num[i:i + 3])   # string comparison matches numeric order
        return best

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n): one pass over the windows of length 3.
💾 Space Complexity
O(1): a single best string of length 3.

⚠️ Interview Pitfalls & Follow-ups

  • Converting to integers: unnecessary, and num can be 1000 digits long. Comparing the fixed-length digit strings lexicographically gives the same ordering.
  • Requiring the substring to be maximal in length: the problem specifically asks for exactly three identical digits.
  • Returning "000" as absent: "000" is a valid answer (it corresponds to the number 0), so the empty-string check must be on the length, not on the numeric value.
  • Missing the last window: range(len(num) - 2) is the correct bound.