C++|sliding window | small an concise soln easy to understand
class Solution {\npublic:\n    int subarraysWithKDistinct(vector& A, int K) {\n        int ans=0;\n        int left=0;\n        int right=0;\n        unordered_
class Solution {\npublic:\n    int subarraysWithKDistinct(vector<int>& A, int K) {\n        int ans=0;\n        int left=0;\n        int right=0;\n        unordered_map<int,int> umap;\n        int cnt=0;\n        \n        for(int i=0;i<A.size();i++){\n            \n            //updating map\n\t\t\t\n            if(umap.find(A[i])==umap.end() || umap[A[i]]==0) cnt++;\n            umap[A[i]]++;\n                \n            \n            //setting left most side for window\n            \n            if(cnt>K){\n                umap[A[right]]--;\n                cnt--;\n                left=right+1;\n                right=left;\n                \n            }\n\t\t\t\n\t\t\t//setting right most side of window and and adding possible number of solution to ans\n\t\t\t\n            if(cnt==K){\n                while(umap[A[right]]>1){\n                    umap[A[right]]--;\n                    right++;\n                }\n                ans=ans+(right-left+1);\n                \n            }\n            \n            \n            \n        }\n        \n        return ans;\n        \n    }\n};
[]