Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

Every qualifying three-character substring is determined by one repeated digit. Comparing those digits is enough to compare the candidate numbers, while returning a string preserves a valid result such as 000.

Approach

Scan every length-three window and check whether all its characters match. Track the greatest matching digit, using a sentinel to distinguish no match from digit zero. Return that digit repeated three times, or an empty string if no window qualifies. Longer runs are harmless: their overlapping windows represent the same candidate and do not affect the maximum.

class Solution: def largestGoodInteger(self, num: str) -> str: n = len(num) res = -1 for i in range(2, n): if num[i - 2] == num[i - 1] and num[i - 1] == num[i]: res = max(res, int(num[i])) return "" if res == -1 else str(res) * 3

Complexity analysis

Let n = num.length. Time: O(n) for one scan of all length-three windows. Auxiliary space: O(1).