https://leetcode.com/problems/largest-3-same-digit-number-in-string/description/
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
1
2
3
4
5
6
7
8
|
class Solution:
def largestGoodInteger(self, num: str) -> str:
worst_case = ''
for i in range(9, -1, -1):
if str(i)*3 in num:
return str(i)*3
return worst_case
|
cs |
문자열 첫 글자부터 한 글자씩 이동하면서 000, 111,222 인지 확인하는 방법도 있긴한데..
차라리 문자열 전체에 '999' -> '888'.. 있는지 확인하는게 더 직관적이라서 저렇게 풀었습니더
There is also a way to check whether it is 000, 111,222 by moving one character from the first character of the string.
Rather, it is more intuitive to check whether the entire string is '999' -> '888'..
반응형
'python-algorithm' 카테고리의 다른 글
백준 27866 문자와 문자열 (0) | 2023.03.13 |
---|---|
leetcode 2451. Odd String Difference (0) | 2023.03.08 |
leetcode 2549. Count Distinct Numbers on Board (0) | 2023.03.07 |
leetcode 1539. Kth Missing Positive Number (0) | 2023.03.06 |
leetcode 2164. Sort Even and Odd Indices Independently (0) | 2023.03.03 |
댓글