aboutsummaryrefslogtreecommitdiff
path: root/largest_3_digit_string.py
blob: f8789a41ca484f30b1bd6f8f56b9848b736eccb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 2264. Largest 3-Same-Digit Number in String

"""
given a string 'num' representing a large integer, an integer is good if it
meets the following conditions: if 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.
"""


class Solution(object):
    def largestGoodInteger(self, num):
        """
        :type num: str
        :rtype: str
        """
        ans = ""
        empty = ""
        tmp = 0
        check = True
        for i in range(len(num) - 2):
            if i + 2 < len(num) and num[i] == num[i + 1] == num[i + 2]:
                tmp = max(tmp, int(num[i]))
                check = False
        if not check:
            ans += str(tmp) * 3
        else:
            return empty
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.largestGoodInteger("6777133339"))  # expect: 777
    print(obj.largestGoodInteger("2300019"))  # expect: 000
    print(obj.largestGoodInteger("42352338"))  # expect: NULL