Leetcode编程C语言之1668. 最大重复子字符串
给你一个字符串 sequence ,如果字符串 word 连续重复 k 次形成的字符串是 sequence 的一个子字符串,那么单词 word 的 重复值为 k 。单词 word 的 最大重复值 是单词 word 在 sequence 中最大的重复值。如果 word 不是 sequence 的子串,那么重复值 k 为 0 。
给你一个字符串 sequence 和 word ,请你返回 最大重复值 k 。
#define Max(a, b) (a > b? a:b)
bool mystrcmp(char *s, char *t){
int i, j;
for(i=0, j=0; s[i]!='\0'&&t[j]!='\0'&&s[i]==t[j]; i++, j++);
return t[j]=='\0'? true:false;
}
int maxRepeating(char *sequence, char *word)
{
if(strlen(sequence) < strlen(word))
return 0;
int maxrepeat=0;
for(int i=0; sequence[i] != '\0'; i++){
int j = i;
int count = 0;
while(mystrcmp(sequence+j, word)){
count++;
j += strlen(word);
}
maxrepeat = Max(count, maxrepeat);
if(j >= strlen(sequence))
break;
}
return maxrepeat;
}