[Java] ✅ 3MS ✅ 100% ✅ SLIDING WINDOW ✅ FASTEST ✅ BEST
Approach\n1. Expand a window (left, right) while char[right] != char[right + 1]\n2. Inside this valid window you have n * (n+1) /2 substrings. (1 + right - left
# Approach\n1. Expand a window (left, right) while char[right] != char[right + 1]\n2. Inside this valid window you have n * (n+1) /2 substrings. (1 + right - left)\n3. Adjust left and right to right +1 (window starts at next position where right stopped)\n\n# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public long countAlternatingSubarrays(int[] nums) {\n        long count = 0L;\n        int left = 0, right = 0;\n        while (left < nums.length) {\n            while (right < nums.length - 1 && nums[right] != nums[right + 1]) {\n                right++;\n            }\n            count += (long)(1 + right - left) * (2 + right - left) / 2;\n            left = ++right;\n        }\n        return count;\n    }\n}\n```
['Sliding Window', 'Java']