Skip to content
On this page

830. Positions of Large Groups

https://leetcode.com/problems/positions-of-large-groups/

js
/**
 * @param {string} S
 * @return {number[][]}
 */
var largeGroupPositions = function(S) {
  let pos = []
  let current = ''
  let length = 0
  let start = 0

  S.split('').forEach((l, i) => {
    if (l === current) {
      length++
    } else {
      if (length >= 3) {
        pos.push([start, i - 1])
      }
      current = l
      length = 1
      start = i
    }
  })

  if (length >= 3) {
    pos.push([start, S.length - 1])
  }

  return pos
}
py
class Solution:

    def largeGroupPositions(self, S):
        """
        :type S: str
        :rtype: List[List[int]]
        """
        pos = []
        current = ''
        length = 0
        start = 0

        for i, l in enumerate(S):
            if l == current:
                length += 1
            else:
                if length >= 3:
                    pos.append([start, i - 1])
                current = l
                length = 1
                start = i

        if length >= 3:
            pos.append([start, i])

        return pos