DP Solution if you need
\n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(1)\n\n# Code\n\nfunc countAlternatingSubarrays(nums []int) int64 {\n    return foo(nums, 0)\n}\
\n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(1)\n\n# Code\n```\nfunc countAlternatingSubarrays(nums []int) int64 {\n    return foo(nums, 0)\n}\nfunc foo(nums []int, index int) int64 {\n    if index == len(nums) {\n        return 0\n    }\n    var sum int64 = 0\n    for i := index; i < len(nums); i ++ {\n        if i == 0 {\n            sum += int64(1)\n            continue\n        }\n        if nums[i] != nums[i - 1] {\n            sum += int64(i - index + 1)\n        } else {\n            z := foo(nums[i:], 0)\n            sum += z\n            return sum\n        }\n    }\n    return sum\n}\n```
['Dynamic Programming', 'Recursion', 'Go']