[
two pointers
groupby
accumulate
]
Leetcode 0830 Positions of Large Groups
Problem statement
https://leetcode.com/problems/positions-of-large-groups/
Solution
What we need to find are places of groups with equal elements with size >=3
. We can use functionality of python to evaluate sizes of groups and then use cumulative sums to directly get intervals.
Complexity
Time and space complexity is O(n)
.
Code
class Solution:
def largeGroupPositions(self, s):
t = [len(list(j)) for i, j in groupby(s)]
return [[num-t[i], num-1] for i, num in enumerate(accumulate(t)) if t[i]>= 3]
Remark
Also we can just use two pointers approach with sliding window.