最简单的方法:
遍历所有子串,将所有出现过的子串存储在HashMap中。键值记录所有子串,值记录出现的次数。好处是既可以记录重复子串,也可以记录重复次数。
Map<String,Integer> map = new HashMap<String,Integer>();
for(int i = 0; i < len; i++){
for(int j = i; j < len; j++){
String st0 = str.substring(i,j+1);
if(map.containsKey(st0)){//是否包含了这个值
//map.containsValue(value)
map.put(st0,map.get(st0)+1);//根据键(字符子串),记录重复次数
}else{
map.put(st0,1);
}
}
}
for(Map.Entry<String,Integer> m0 : map.entrySet()){
//System.out.println(m0.getKey()+" "+m0.getValue());
if(m0.getValue() >= 2 && m0.getKey().length() > 2){//次数大于二,并且长度大于二
//System.out.println(m0.getKey()+" "+m0.getValue());
return false;
}
}