llllmz

导航

459. 重复的子字符串 c

 

bool repeatedSubstringPattern(char* s) {
    int ns=0;
    while(s[ns]!=0) ns++;
    if(ns<=1) return false;
    bool same =true;
    char temp=s[0];
    int i=1;
    for(;i<ns;i++){
        if(s[i]!=temp) same=false;
        if(ns%i==0 && i!=1) break;
    }
    if(i==ns) return same;
    int slowhead=0,fasthead=1;
    while(fasthead<=ns/2){
        int slow=slowhead,fast=fasthead;
        while(s[slow]==s[fast]){
            slow++;
            fast++;
            if(fast==ns)  return true;
        }
        fasthead++;
    }
    if(fasthead>ns/2) return false;
    return true;
}

 

结果:

posted on 2024-02-29 17:36  神奇的萝卜丝  阅读(15)  评论(0)    收藏  举报