Largest 3-Same-Digit Number in String - Problem

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 num with length 3.
  • 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 num or a good integer.

Input & Output

Example 1 — Multiple Good Integers
$ Input: num = "6777133"
Output: "777"
💡 Note: The good integers are "777" and "333". Since "777" > "333", we return "777".
Example 2 — No Good Integer
$ Input: num = "2300019"
Output: "000"
💡 Note: The only good integer is "000" (three consecutive zeros).
Example 3 — No Valid Triplets
$ Input: num = "42352338"
Output: ""
💡 Note: No substring of length 3 consists of only one unique digit.

Constraints

  • 3 ≤ num.length ≤ 1000
  • num only consists of digits.

Visualization

Tap to expand
Largest 3-Same-Digit Number in StringInput:6777133Valid 3-digit sequences:"777""133" (not all same)Compare and select maximum:Result: "777"
Understanding the Visualization
1
Input
String containing digits: "6777133"
2
Process
Find all 3-character substrings with identical digits
3
Output
Return the lexicographically largest: "777"
Key Takeaway
🎯 Key Insight: Scan for consecutive identical triplets and track the lexicographically largest one
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K 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