Python || O(1) Space🔥🔥|| DP
\n# Approach 1 (Uses O(N) Time)\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g.
\n# Approach 1 (Uses O(N) Time)\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def countAlternatingSubarrays(self, nums: List[int]) -> int:\n        dp=[1]*len(nums) #Dp[i] represents num of subarrays upto i.\n        \n        for i in range(1,len(nums)):\n            if nums[i]!=nums[i-1]: # if curr elemenet is not equal to previous element, then it can be the part of our subarray\n                \n                dp[i]=dp[i-1]+dp[i]  # total subarrays upto prev+subarray with 1 current element \n            \n        return sum(dp)\n        \n```\n# Approach 2 (Uses O(1) Space)\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# Code\n```\nclass Solution:\n    def countAlternatingSubarrays(self, nums: List[int]) -> int:\n        #dp=[1]*len(nums)\n        s=1 # sum with only \n        curr=1\n        for i in range(1,len(nums)):\n            if nums[i]!=nums[i-1]:\n                curr=curr+1\n                s+=curr\n            else:\n                curr=1\n                s+=curr\n                \n        return s\n```
['Dynamic Programming', 'Python3']