/**
* Author:SunLala
* Date: 2022/5/10
* 功能描述:(
* 给你一个由小写字母组成的长度为n的字符串 S ,找出所有长度为 k 且包含重复字符的子串,请你返回全部满足要求的子串的数目。
*
* 数据范围: 2 <= k <= 400 , 5<= n <= 900
* 进阶: 时间复杂度O(n)O(n),空间复杂度O(n)O(n))
*/
public class Test16 {
public static void main(String[] args) {
System.out.println(numKLenSubstrRepeats("createfunonyoka",4));
System.out.println(numKLenSubstrRepeats("yokagames",3));
System.out.println(numKLenSubstrRepeats("yoka",0));
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param k int整型
* @return int整型
*/
public static int numKLenSubstrRepeats (String s, int k) {
// write code here
if(s.length() == 0 || s == null){ //处理特殊情况
return 0;
}
int count = 0;
for(int i = 0; i<= s.length()-k; i++){ //遍历字符串
String temp = s.substring(i,i+k);
for(int j = 0; j < k; j++){
if(!(j == temp.lastIndexOf(temp.charAt(j)))){ //判断当前字符是否等于该字符的最后一个索引,如果不等说明有重复字符,则count+1
count++;
break;
}
}
}
return count;
}
}
总结:
1.不要被题目知识点1提示吓到,刚看到双指针时,一直在想怎么用双指针写,把自己困住了,今天按自己的思路仍然也可以成功
2. 如果判断一个字符串中有没有重复字符,方法:遍历该字符串,如果当前字符的最后一个索引不等于当前索引,说明有重复的