Problem Solving/LeetCode
2023.12.04 Today's Challenge
fabichoi
2023. 12. 4. 23:45
https://leetcode.com/problems/largest-3-same-digit-number-in-string/
Largest 3-Same-Digit Number in String - LeetCode
Can you solve this real interview question? Largest 3-Same-Digit Number in String - You are given a string num representing a large integer. An integer is good if it meets the following conditions: * It is a substring of num with length 3. * It consists of
leetcode.com
좀 더 리펙토링을 할수도 있지만.... 여기서 그만
class Solution:
def get_number(self, num):
return str(num) if num[0] == num[1] == num[2] else ""
def largestGoodInteger(self, num: str) -> str:
if len(num) == 3:
return self.get_number(num)
res = ""
for i in range(len(num)-2):
n = self.get_number(num[i:i+2+1])
if n:
if not res:
res = n
continue
if int(res) < int(n):
res = n
return res
반응형