LeetCode solutions

3463. Alternating Groups I

My accepted Python solution to LeetCode problem 3463, Alternating Groups I, running in 60ms.

  • Difficulty: Easy
  • Python
  • Runtime 60ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 60ms, memory 17.3MB, accepted 2025-12-30.

python
class Solution:
    def numberOfAlternatingGroups(self, colors: List[int]) -> int:
        n = len(colors)
        count = 0
        for i in range(n):
            left = colors[(i - 1) % n]
            mid = colors[i]
            right = colors[(i + 1) % n]
            if mid != left and mid != right:
                count += 1
        return count

Source