Intuition
Try working out a couple of custom examples on paper first.To start, consider 0101. Brute force it. How many alternating subarrays are there? Well, we can split
Try working out a couple of custom examples on paper first.

To start, consider 0101. Brute force it. How many alternating subarrays are there? Well, we can split it into different groups of length 1, length 2, length 3, and length 4. Respectively, 4, 3, 2, 1 of each. Therefore the result will be 4 + 3 + 2 + 1. So, we can have an adding_factor, starting at 1, since every element by itself is a valid alternating subarray. When we move the index to the next element, check if it is different than the previous. If it is different, add 1 to the adding factor then add the adding factor to the final answer. When we encounter a same element again on the other hand, we can just reset the adding factor to one, since subarrays have to be contiguous and therefore when we encounter a same element we can just discard the lengths of the previous.

```python
class Solution:
    def countAlternatingSubarrays(self, nums: List[int]) -> int:
        n = len(nums)
        i = 1
        ans = 1
        adding_factor = 1
        while i < n:
            if nums[i] != nums[i - 1]:
                adding_factor += 1
            else:
                adding_factor = 1
            ans += adding_factor
            i += 1
        return ans
```
['Greedy', 'Python3']