Given a string num representing a large integer, return the largest good integer as a string. A good integer is a substring of num with exactly 3 consecutive digits that are the same.
If no such integer exists, return an empty string "".
The most straightforward approach is to iterate through the string and check for 3 consecutive same digits.
Algorithm:
- Initialize an empty result string
- Iterate through the string from index 0 to
length - 3 - For each position, check if the current digit and next two digits are the same
- If found, extract the substring and update result if it's larger lexicographically
- Return the result
Time Complexity: O(n) - where n is the length of the string Space Complexity: O(1) - constant extra space
A more explicit approach using a sliding window of size 3.
Algorithm:
- Initialize an empty result string
- Use a sliding window of size 3 to check consecutive digits
- Compare all three characters in the window
- If they're the same, form the substring and update result if larger
- Return the result
Time Complexity: O(n) - where n is the length of the string Space Complexity: O(1) - constant extra space
- Consecutive Check: We need exactly 3 consecutive same digits, not just 3 occurrences
- Lexicographical Order: String comparison works naturally for digits (e.g., "999" > "888")
- Empty String: Return empty string if no good integer is found
- Boundary Check: Only iterate up to
length - 3to avoid index out of bounds
- num = "6777133339":
- Found "333" at position 5
- Found "777" at position 1
- "777" > "333" lexicographically, so return "777"
- num = "2300019":
- Found "000" at position 2
- Only one good integer, so return "000"
- num = "42352338":
- No 3 consecutive same digits found
- Return ""
- String length < 3: Return empty string
- No good integers: Return empty string
- Multiple good integers: Return the lexicographically largest
- All same digits: Return the first 3 digits
Both solutions are clean and efficient:
- Simple Iteration: More straightforward and easier to understand
- Sliding Window: More explicit about the window concept
The sliding window approach makes the logic more clear and can be easily extended to other similar problems that require checking consecutive elements.
- Both approaches have the same time and space complexity
- String comparison is efficient for small strings
- The solution handles all edge cases correctly
- No need for complex data structures or algorithms