Largest 3-Same-Digit Number in String - Problem

You are given a string num representing a large integer. Your task is to find the largest "good" integer within this string.

A good integer is defined as:

  • A substring of exactly 3 consecutive characters
  • All 3 characters must be the same digit

Return the maximum good integer as a string, or an empty string "" if no such integer exists.

Note: Leading zeros are allowed in both the input and output. For example, "000" is a valid good integer and is smaller than "111".

Examples:

  • "6777133339""777" (largest among "777" and "333")
  • "2300019""000"
  • "42352338""" (no consecutive identical digits)

Input & Output

example_1.py — Basic case with multiple good integers
$ Input: "6777133339"
Output: "777"
💡 Note: The string contains two good integers: "777" and "333". Since "777" > "333" lexicographically, we return "777".
example_2.py — Good integer with zeros
$ Input: "2300019"
Output: "000"
💡 Note: The string contains one good integer: "000". Even though it contains zeros, it's still a valid good integer.
example_3.py — No good integers
$ Input: "42352338"
Output: ""
💡 Note: There are no three consecutive identical digits in this string, so we return an empty string.

Constraints

  • 3 ≤ num.length ≤ 1000
  • num only consists of digits
  • Leading zeros are allowed in the result

Visualization

Tap to expand
String: "6777133339"677713339Good Integer: "777"Good Integer: "333"Result: "777" (larger than "333")
Understanding the Visualization
1
Start scanning
Begin at the first position and examine groups of 3 consecutive characters
2
Check for matches
At each position, verify if the current character matches the next two
3
Update maximum
When a good integer is found, compare it with the current best and update if larger
4
Continue to end
Keep scanning until all possible positions are checked
Key Takeaway
🎯 Key Insight: A single pass through the string checking consecutive characters is sufficient to find the maximum good integer in O(n) time.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 15
26.8K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen