Problem Description
Given a numeric string, find the largest substring of length 3 in which all characters are the same. If no such substring exists, return an empty string.
Key Insights
- Loop through the string and check every substring of length 3.
- Validate if all three characters are identical.
- Keep track of the maximum valid substring based on lexicographical (and numerical) order.
- Since the digits range from 0-9, lexicographic comparison suffices.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the input string. Space Complexity: O(1).
Solution
Iterate over the string by examining every contiguous substring of length 3. For each substring, check if all characters are the same by comparing the first character with the second and third. If valid, compare this substring with the currently stored maximum; update if it's larger. Return the maximum valid substring after processing the entire string.