In a string S
of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like S = "abbxxxxzyy"
has the groups "a"
, "bb"
, "xxxx"
, "z"
and "yy"
.
Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group.
The final answer should be in lexicographic order.
给出字符串,让你找连续相同的长度大于等于3的子字符串。
class Solution(object): def largeGroupPositions(self, S): """ :type S: str :rtype: List[List[int]] """ l = 0 r = 0 n = len(S) ans = [] while r < n: while r < n and S[l] == S[r]: r += 1 length = r - l if length >= 3 and length: ans.append([l, r - 1]) l = r return ans