diff options
Diffstat (limited to 'largest_3_digit_string.py')
-rw-r--r-- | largest_3_digit_string.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/largest_3_digit_string.py b/largest_3_digit_string.py new file mode 100644 index 0000000..f8789a4 --- /dev/null +++ b/largest_3_digit_string.py @@ -0,0 +1,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 |