LeetCode #830. Positions of Large Groups
题目
830. Positions of Large Groups
解题方法
遍历数组,维护当前字符curletter,当前字符的个数curlettercount和返回值rat,如果当前字符不是curletter,就看看curlettercount是多少,超过3的话就把起始和终止位置加入到返回值rat里,最后还需要看看结尾处是不是一个连续的字符组。
时间复杂度:O(n)
空间复杂度:O(1)
代码
class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
curletter = s[0]
curlettercount = 1
rat = []
for i in range(1, len(s)):
if s[i] == curletter:
curlettercount += 1
else:
if curlettercount >= 3:
rat.append([i-curlettercount, i-1])
curletter = s[i]
curlettercount = 1
else:
if curlettercount >= 3:
rat.append([i+1-curlettercount, len(s)-1])
return rat