2264. Largest 3-Same-Digit Number in String - Explanation
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
numwith length3. - 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
numor 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 <= 1000numonly consists of digits.
Topics
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
- Initialize
resas an empty string andvalas0to track the largest good integer found. - Iterate through the string from index
0tolen(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 bothvalandres.
- Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
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
- Initialize
resto"0"as a baseline for comparison. - Iterate through the string, checking each window of size 3:
- If three consecutive characters are the same, compare the substring with
resand keep the larger one.
- If three consecutive characters are the same, compare the substring with
- If
resis still"0"and"000"was never found, return an empty string. Otherwise, returnres.
Time & Space Complexity
- Time complexity:
- Space complexity:
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
- Initialize
resto-1to indicate no good integer found yet. - Iterate through the string, checking each window of size 3:
- If three consecutive characters are the same, extract the digit value and update
resif it is larger.
- If three consecutive characters are the same, extract the digit value and update
- If
resis still-1, return an empty string. Otherwise, return the digit repeated three times.
Time & Space Complexity
- Time complexity:
- Space complexity:
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.
Sign in to join the discussion