|
| 1 | +''' |
| 2 | +In a string S of lowercase letters, these letters form consecutive groups of the same character. |
| 3 | + |
| 4 | +For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy". |
| 5 | + |
| 6 | +Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group. |
| 7 | + |
| 8 | +The final answer should be in lexicographic order. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +Example 1: |
| 13 | + |
| 14 | +Input: "abbxxxxzzy" |
| 15 | +Output: [[3,6]] |
| 16 | +Explanation: "xxxx" is the single large group with starting 3 and ending positions 6. |
| 17 | +Example 2: |
| 18 | + |
| 19 | +Input: "abc" |
| 20 | +Output: [] |
| 21 | +Explanation: We have "a","b" and "c" but no large group. |
| 22 | +Example 3: |
| 23 | + |
| 24 | +Input: "abcdddeeeeaabbbcd" |
| 25 | +Output: [[3,5],[6,9],[12,14]] |
| 26 | +''' |
| 27 | + |
| 28 | +class Solution(object): |
| 29 | + def largeGroupPositions(self, S): |
| 30 | + """ |
| 31 | + :type S: str |
| 32 | + :rtype: List[List[int]] |
| 33 | + """ |
| 34 | + if not S: |
| 35 | + return [] |
| 36 | + |
| 37 | + result = [] |
| 38 | + count = 1 |
| 39 | + prevChar = S[0] |
| 40 | + index_i = 0 |
| 41 | + for index in range(1,len(S)): |
| 42 | + if S[index] == prevChar: |
| 43 | + count += 1 |
| 44 | + else: |
| 45 | + if count >= 3: |
| 46 | + result.append([index_i, index-1]) |
| 47 | + |
| 48 | + count = 1 |
| 49 | + prevChar = S[index] |
| 50 | + index_i = index |
| 51 | + |
| 52 | + if count >= 3: |
| 53 | + result.append([index_i, len(S)-1]) |
| 54 | + return result |
0 commit comments