-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.py
More file actions
36 lines (26 loc) · 1.17 KB
/
solution.py
File metadata and controls
36 lines (26 loc) · 1.17 KB
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
class Solution:
def largestGoodInteger(self, num: str) -> str:
result = ""
# Check for 3 consecutive same digits starting from the left
for i in range(len(num) - 2):
# Check if current position and next two positions have the same digit
if num[i] == num[i + 1] == num[i + 2]:
current = num[i:i + 3]
# Update result if current is larger (lexicographically)
if current > result:
result = current
return result
# Alternative approach using sliding window
class Solution2:
def largestGoodInteger(self, num: str) -> str:
result = ""
# Use sliding window of size 3
for i in range(len(num) - 2):
c1, c2, c3 = num[i], num[i + 1], num[i + 2]
# Check if all three characters are the same
if c1 == c2 == c3:
current = c1 + c2 + c3
# Update result if current is larger
if not result or current > result:
result = current
return result