Largest 3-Same-Digit Number in String

You are given a string `num` of digits. An integer is good if it is a substring of `num` of length 3 that consists of one unique digit. Return the maximum good integer as a string, or an empty string `""` if no good integer exists. A substring is a contiguous sequence of characters, and `num` or a good integer may have leading zeros.

30:00

Largest 3-Same-Digit Number in String
easy
Free written solution
Topics
Companies

You are given a string num of digits. An integer is good if it is a substring of num of length 3 that consists of one unique digit. Return the maximum good integer as a string, or an empty string "" if no good integer exists. A substring is a contiguous sequence of characters, and num or a good integer may have leading zeros.

Example 1:
Input: {"num":"6777133339"}
Output: 777
Constraints:
  • 3 <= num.length <= 1000

  • num only consists of digits.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"num":"6777133339"}

best starts empty. Slide a window of 3 digits across num (8 positions) and keep the largest window whose digits are all equal.

num with a 3-digit window

8 windows

6

0

7

 

7

 

7

 

1

 

3

5

3

 

3

 

3

 

9

 

good integers seen (largest wins)

999

888

777

666

555

444

333

222

111

000

best =

""

0 good windows

Variables
VariableValue
best""
windows8
DepthFunction Call
Stack empty
0/10