class Solution { public: /** * @param stringIn: The original string. * @param K: The length of substrings. * @return: return the count of substring of length K and exactly K distinct characters. */ int KSubstring(string &stringIn, int K) { // Write your code here if (stringIn.length() < K) return 0; set<string> res; int cnt = 0; vector<int> temp(256,0); for (int i = 0; i < stringIn.length();i++) { temp[stringIn[i]]++; if (temp[stringIn[i]] == 1) cnt++; if (i >= K) { if (temp[stringIn[i-K]] == 1) cnt--; temp[stringIn[i-K]]--; } if (cnt == K) res.insert(stringIn.substr(i-K+1,K)); } return res.size(); } };
-------------end of file
thanks for reading-------------