Fastest and Easiest Answer with Explanation || Dynamic Programming || Beats 98.92% of users
Approach\n Describe your approach to solving the problem. \nSolving the problem by using Dynamic Programming\n# Complexity\n- Time complexity:O(n)\n Add your ti
# Approach\n<!-- Describe your approach to solving the problem. -->\nSolving the problem by using Dynamic Programming\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n// Use Dynamic Programming to solve the problem\n// Make a grid like this\n// nums = [0,1,1,1]\n//\n//  |   | 0 | 1 | 1 | 1 |\n//  ---------------------\n//  | 0 |   |   |   |   |\n//  ---------------------\n//  | 1 |   |   |   |   |\n//  ---------------------\n//  | 1 |   |   |   |   |\n//  ---------------------\n//  | 1 |   |   |   |   |\n\n// Then you should iterate from begin to the end of the list\n// And check if the letter before the corrent digit is not\n// the same digit do that add one to the count of the current \n// subarray and put into the index [i][i] of the grid\n// If it is not do that put 1 into the index [i][i] of the grid\n\n//  |   | 0 | 1 | 1 | 1 |\n//  ---------------------\n//  | 0 | 1 |   |   |   |\n//  ---------------------\n//  | 1 |   | 2 |   |   |\n//  ---------------------\n//  | 1 |   |   | 1 |   |\n//  ---------------------\n//  | 1 |   |   |   | 1 |\n\n// each index of the grid tells how many alternating subarray\n// you can make by using this number which is in the index of\n// nums. For example: in second column there is 2. It means\n// alternating subarray by using the number 1: 0,1 and 1.\n\n// After filling the grid, return sum of the number of \n// alternating subarray by using the number which is in the index of nums.\n\n\nfunc countAlternatingSubarrays(nums []int) int64 {\n    numberOfSubarray := 1\n    sizeOfLastSabarray := 1\n    for i := 1; i < len(nums); i++ {\n        // Ckeck alternating or not\n        if nums[i] != nums[i-1] {\n            sizeOfLastSabarray += 1\n        }else {\n            sizeOfLastSabarray = 1\n        }\n        // Add the number of alternating subarray \n        // by using the number which is in the index of nums\n        numberOfSubarray += sizeOfLastSabarray\n    }\n\n    return int64(numberOfSubarray)\n}\n```
['Dynamic Programming', 'Go']