[Python] Sliding Window | Set + HashMap
\nclass Solution:\n    def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:\n        \n        def window(nums, k):\n            left = 0\n        
```\nclass Solution:\n    def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:\n        \n        def window(nums, k):\n            left = 0\n            right = 0\n\n            res = 0\n            in_set = set()\n            hash_map = collections.Counter()\n\n            while right < len(nums):\n                in_set.add(nums[right])\n                hash_map[nums[right]] += 1\n\n                while len(in_set) > k:\n                    hash_map[nums[left]] -= 1\n                    if hash_map[nums[left]] == 0:\n                        in_set.remove(nums[left])\n                    left += 1\n\n                res += (right - left + 1)\n\n                right += 1\n            return res\n    \n       return window(nums, k) - window(nums, k - 1)\n```
['Ordered Set', 'Python', 'Python3']