↓Skip to main content
  1. LeetCode/

LeetCode 2264: Largest 3-Same-Digit Number in String

·1 min· ·
LeetCode Easy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-08-14

  • Submissions: Accepted (first try)

Approach
#

A good integer must look like "ddd", so there are only 10. From the largest, "999", down, the first one found in num is the answer (String Template 3).

My Solution
#

I originally used a length-3 sliding window with a Counter, comparing with the current answer whenever the window held one character. Enumerating the 10 candidates is simpler:

class Solution:
    def largestGoodInteger(self, num: str) -> str:
        for d in "9876543210":
            if d * 3 in num:
                return d * 3
        return ""

Complexity
#

Time O(10 · n), space O(1).

Mistakes to Avoid
#

  • The answer is a string, and "000" is valid; do not convert to an integer.

Interview Explanation
#

Good integers are only "000" through "999", so I check from largest to smallest which appears in the string; the first found is the answer. Time O(n).

Related

LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent
LeetCode 3541: Find Most Frequent Vowel and Consonant
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 3541 note: count characters, take the maximum among vowels and among consonants and add them; a missing class counts as 0
LeetCode 3606: Coupon Code Validator
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 3606 note: filter valid coupons, then sort by (category order, code); character ranges must include z, Z and 9
LeetCode 1768: Merge Strings Alternately
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 1768 note: take characters in turn with one index, then append whatever remains of the longer string
LeetCode 2000: Reverse Prefix of Word
·1 min
LeetCode Easy Two-Pointers String Stack
LeetCode 2000 note: find the first index i of ch, reverse word[:i + 1] and append the rest; return the word unchanged if ch is absent
LeetCode 3120: Count the Number of Special Characters I
·1 min
LeetCode Easy Hash-Table String
LeetCode 3120 note: collect the letters seen in lowercase and in uppercase; the size of their intersection is the answer