-
Notifications
You must be signed in to change notification settings - Fork 43
/
positions-of-large-groups.py
104 lines (99 loc) · 2.52 KB
/
positions-of-large-groups.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# V0
# V1
# IDEA : STRING
class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
res = []
i = 0
while i < len(S):
j = i
while j < len(S) and S[i] == S[j]:
j += 1
if j - i >= 3:
res.append([i, j - 1])
i = j
else:
i += 1
return res
# V1'
# http://bookshadow.com/weblog/2018/05/06/leetcode-positions-of-large-groups/
# IDEA : TWO POINTERS
class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
j = -1
d = ''
ans = []
for i, c in enumerate(S + '#'):
if c != d:
if i - j >= 3:
ans.append([j, i - 1])
j = i
d = c
return ans
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/80472242
# IDEA : GREEDY
class Solution:
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
groups = []
before_index, before_char = 0, S[0]
for i, s in enumerate(S):
if s != before_char:
if i - before_index >= 3:
groups.append([before_index, i - 1])
before_index = i
before_char = s
if i - before_index >= 2:
groups.append([before_index, i])
return groups
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/80472242
# IDEA : GREEDY
class Solution:
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
S = S + "A"
groups = []
previndex, prevc = 0, ""
for i, c in enumerate(S):
if not prevc:
prevc = c
previndex = i
elif prevc != c:
if i - previndex >= 3:
groups.append([previndex, i - 1])
previndex = i
prevc = c
return groups
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
result = []
i = 0
for j in range(len(S)):
if j == len(S)-1 or S[j] != S[j+1]:
if j-i+1 >= 3:
result.append([i, j])
i = j+1
return result