给你一个由小写字母组成的长度为n的字符串 S ,找出所有长度为 k 且包含重复字符的子串,请你返回全部满足要求的子串的数目。
数据范围:
,
进阶: 时间复杂度
,空间复杂度)
把字符串看成一条长为n的线,一个长为k的毛毛虫从线上爬过去,初始位置为[0,k-1] 用双循环检查毛毛虫内部有没有重复的字母,有就跳过双循环,答案+1 毛毛虫向前爬一步(left+1和right+1) 直到爬到终点(right == n) int numKLenSubstrRepeats(string s, int k) { int left = 0,right = k-1; int ans = 0; int n = s.size(); bool YN = false; while(right != n) { for(int i=left;i<=right;i++) { for(int j=i+1;j<=right;j++) { if(s[i] == s[j]) { ans++; YN=true; break; } } if(YN) { YN = false; break; } } left++; right++; } return ans; }
public int numKLenSubstrRepeats (String s, int k) { // write code here int n = s.length(); int res = 0; Set<Character> set = new HashSet<>(); for(int i = k-1; i < n; i++){ for(int j = 0; j < k; j++){ set.add(s.charAt(i-j)); } if(set.size() < k){ res++; } set.clear(); } return res; }
class Solution: def numKLenSubstrRepeats(self , s , k ): # write code here num = 0 for i in range(len(s)-k+1): if len(set(s[i:i+k])) < k: num += 1 return num
int numKLenSubstrRepeats(string s, int k) { // T(n) = O(n), S(n) = O(n) if (k == 0) { return 0; } int l = 0; int repeated = 0; int n = s.size(); unordered_map<char, int> mp; int ret = 0; for (int r = 0; r < n; ++r) { mp[s[r]]++; if (mp[s[r]] == 2) { ++repeated; } if (k > 0) { --k; } else { mp[s[l]]--; if (mp[s[l]] == 1) { --repeated; } ++l; } if (k == 0 && repeated > 0) { ++ret; } } return ret; }
public class NumKLenSubstrRepeats { public int numKLenSubstrRepeats(String s, int k) { // write code here // 问题一:如何判断最后一串数字 Set<Character> set = new HashSet<>(); char[] chars = s.toCharArray(); int count = 0; for (int i = 0; i <= chars.length - k; i++) { set.add(chars[i]); for (int j = i + 1; j < i + k; j++) { if (!set.contains(chars[j])) { set.add(chars[j]); continue; } else { count++; break; } } set.clear(); } return count; } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param k int整型 * @return int整型 */ unordered_map<char, int> hash; bool check() { // 判断是否有出现两次的字符 for(auto iter = hash.begin(); iter != hash.end(); iter++) { if(iter->second > 1) return true; } return false; } int numKLenSubstrRepeats(string s, int k) { // write code here int len = s.length(); int left = 0, right = 0; int res = 0; while(right < len) { // 记录出现过的字符次数 hash[s[right]]++; // 间隔为给定的大小时 查询是否出现两次 if(right-left >= k-1) { // 判断是否有出现两次的字符 if(check()) res++; // 指针右移 对应出现的字符次数减一 hash[s[left++]]--; } right++; } return res; } };
function numKLenSubstrRepeats( s , k ) { // write code here let left = 0 let right = k let count = 0 while(right <= s.length){ let arr = s.slice(left, right).split('') let set = new Set(arr) if(arr.length > set.size) count ++ left ++ right ++ } return count }
public static int numKLenSubstrRepeats(String s, int k) { if (k > s.length()) return 0; int res = 0; int[] dict = new int[26]; for (int i = 0; i < s.length(); i++) { int c = s.charAt(i) - 'a'; dict[c]++; if (i >= k) { int shiftC = s.charAt(i - k) - 'a'; dict[shiftC]--; } // 不能只看 c, 还要看窗口内部剩余的是否有重复, 比如 funone, 4 if (i >= k - 1) for (int e : dict) if (e > 1) { res++; break; } } return res; }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @param k int整型 # @return int整型 # class Solution: def numKLenSubstrRepeats(self, s: str, k: int) -> int: # write code here print(s) res = 0 count = [0 for i in range(26)] repeat_chr = {} for i in range(min(k, len(s))): index = ord(s[i])-ord('a') count[index] += 1 if count[index] > 1: repeat_chr[s[i]] = count[index] if len(repeat_chr) > 0: res += 1 left = 0 right = k while right < len(s): count[ord(s[left])-ord('a')] -= 1 if s[left] in repeat_chr: if repeat_chr[s[left]] <= 2: repeat_chr.pop(s[left]) else: repeat_chr[s[left]] -= 1 left += 1 count[ord(s[right])-ord('a')] += 1 if count[ord(s[right])-ord('a')] > 1: repeat_chr[s[right]] = count[ord(s[right])-ord('a')] right += 1 if len(repeat_chr) > 0: res += 1 return res
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param k int整型 * @return int整型 */ public int numKLenSubstrRepeats (String s, int k) { // write code here HashMap<Character, Integer> map = new HashMap<>(); int right = 0; int left = 0; int res = 0; while(right < s.length()) { int times = map.getOrDefault(s.charAt(right), 0); if(right - left + 1 > k || times > 0) { map.put(s.charAt(left), map.get(s.charAt(left)) - 1); left++; continue; } map.put(s.charAt(right), times + 1); if(right - left + 1 == k) res++; right++; } return s.length() - k + 1 - res; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param k int整型 * @return int整型 */ public int numKLenSubstrRepeats (String s, int k) { // write code here int sum=0; ArrayList<Character> list=new ArrayList<>(); //依次进行判断 for(int i=0;i+k<=s.length();i++){ //如果该截取的字符串有重复的就sum++ if(dc(s.substring(i,i+k))==true){ sum++; } } return sum; } public boolean dc(String str){ //HashSet进行查重 HashSet<Character> set=new HashSet<>(); for(int i=0;i<str.length();i++){ if(!set.contains(str.charAt(i))){ set.add(str.charAt(i)); }else{ //有重复 return true; } } //无重复 return false; } }
package main import _"fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param k int整型 * @return int整型 */ func numKLenSubstrRepeats( s string , k int ) int { cnt:=map[byte]int{} ans:=0 for i,ch:=range []byte(s){ if i==k{ break } cnt[ch]++ } if len(cnt)<k{ ans++ } for l,r:=0,k;r<len(s);l,r=l+1,r+1{ var key byte key=byte(s[l]) if cnt[key]==1{ delete(cnt,key) }else{ cnt[key]-- } key=byte(s[r]) cnt[key]++ if len(cnt)<k{ ans++ } } return ans }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param k int整型 * @return int整型 */ public int numKLenSubstrRepeats (String s, int k) { // write code here int ans = 0, flag = 0; int[] cnts = new int[26]; for (int i = 0; i < s.length(); i++) { if (++cnts[s.charAt(i) - 'a'] == 2) ++flag; if (i < k - 1) continue; if (i >= k && --cnts[s.charAt(i - k) - 'a'] == 1) --flag; if (flag > 0) ++ans; } return ans; } }