原题题目
代码实现(首刷自解)
class Solution {
public:int numKLenSubstrNoRepeats(string s, int k) {
if(k > 26 || s.size() < k) return 0;vector<int> cnt(26,0);unordered_set<char> set;int ret = 0,size = s.size();for(int i = 0;i < k;++i){
auto pos = s[i] - 'a';++cnt[pos];if(cnt[pos] >= 2)set.emplace(s[i]);}if(set.empty()) ++ret;for(int i = k;i < size;++i){
++cnt[s[i] - 'a'];--cnt[s[i - k] - 'a'];if(cnt[s[i] - 'a'] >= 2) set.emplace(s[i]);if(cnt[s[i - k] - 'a'] <= 1 && set.find(s[i - k]) != set.end()) set.erase(set.find(s[i - k]));if(set.empty()) ++ret;}return ret;}
};