Skip to content

Commit fd532f3

Browse files
authored
Create largest-3-same-digit-number-in-string.py
1 parent aef8ed1 commit fd532f3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# string
5+
class Solution(object):
6+
def largestGoodInteger(self, num):
7+
"""
8+
:type num: str
9+
:rtype: str
10+
"""
11+
result = ""
12+
cnt = 0
13+
for i, x in enumerate(num):
14+
cnt += 1
15+
if i+1 < len(num) and num[i] == num[i+1]:
16+
continue
17+
if cnt >= 3:
18+
result = max(result, num[i])
19+
cnt = 0
20+
return result*3
21+
22+
23+
# Time: O(n)
24+
# Space: O(1)
25+
# string
26+
class Solution2(object):
27+
def largestGoodInteger(self, num):
28+
"""
29+
:type num: str
30+
:rtype: str
31+
"""
32+
return max(num[i] if num[i] == num[i+1] == num[i+2] else "" for i in xrange(len(num)-2))*3

0 commit comments

Comments
 (0)