Largest 3 same digit number in string

November 2, 2022

array-and-hashmap

Problem URL: Largest 3 same digit number in string

We will start from 9 till 0 and try to find the largest 3 same digit number in the string. If we find it, we return it. Otherwise, we return empty string.

class Solution:
    def largestGoodInteger(self, num: str) -> str:
        for digit in range(9, -1, -1):
            sub = str(digit) * 3
            if sub in num:
                return sub
        return ''

Time complexity: O(n)
Space complexity: O(1)