代码拉取完成,页面将自动刷新
Given a string S, return the number of substrings of length K with no repeated characters.
Example 1:
Input: S = "havefunonleetcode", K = 5
Output: 6
Explanation:
There are 6 substrings they are : 'havef','avefu','vefun','efuno','etcod','tcode'.
Example 2:
Input: S = "home", K = 5
Output: 0
Explanation:
Notice K can be larger than the length of S. In this case is not possible to find any substring.
Note:
1 <= S.length <= 10^4
All characters of S are lowercase English letters.
1 <= K <= 10^4
class Solution {
public:
int t[26];
int numKLenSubstrNoRepeats(string S, int K) {
memset(t,0,sizeof(t));
int res=0;
for(int i=0;i<S.size();i++){
t[S[i]-'a']++;
if(i+1<K)continue;
if(good()){
res++;
}
t[S[i-K+1]-'a']--;
}
return res;
}
bool good(){
for(int i=0;i<26;i++){
if(t[i]>1)return false;
}
return true;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。