abc aabbcc aaabbbccc...之类的字符串被称为beautiful string,现在给定一个字符串,判断它是否包含beautiful string!
public class CheckBeautifulString {
boolean isBeautifulString(String src){
boolean ret = false;
if(src==null||src.length() < 3) return false;
int len = src.length();
int p1 = 0;
while(p1<len){
if(p1+2 < len&&src.charAt(p1)+1==src.charAt(p1+1)&&src.charAt(p1+1)+1==src.charAt(p1+2)){
return true;
}else if (p1+1<len && src.charAt(p1)==src.charAt(p1+1)) {
int count = 2;
int p2 = p1+1;
while(p2+1<len&&src.charAt(p2)==src.charAt(p2+1)){
count++;
p2++;
}//得到长度
p2 = p1;
if(p2+count*3-1 < len){//没有越界,保证足够长度
boolean t = true;
for(int i = 0;i < count;i++){
if(!(src.charAt(p1+i)+1==src.charAt(p1+i+count)&&src.charAt(p1+i+count)+1==src.charAt(p1+i+2*count))){
t = false;
}
}
if(t) return true;
}
}
p1++;
}
return ret;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new CheckBeautifulString().isBeautifulString("aaxabbbcccddd"));
}
}