首页 > 其他分享 >1668. 最大重复子字符串

1668. 最大重复子字符串

时间:2022-11-03 10:37:06浏览次数:30  
标签:word sequence 重复 重复子 int 1668 字符串

1668. 最大重复子字符串

给你一个字符串 sequence ,如果字符串 word 连续重复 k 次形成的字符串是 sequence 的一个子字符串,那么单词 word 的 重复值为 k 。单词 word 的 最大重复值 是单词 word 在 sequence 中最大的重复值。如果 word 不是 sequence 的子串,那么重复值 k 为 0 。

给你一个字符串 sequence 和 word ,请你返回 最大重复值 k 。

  • 输入:sequence = "ababc", word = "ab"
  • 输出:2
  • 解释:"abab" 是 "ababc" 的子字符串。

暴力

class Solution {
public:
    int maxRepeating(string sequence, string word) {
        int res=0;
        for(int i=0;i<sequence.size();i++){
            if(sequence[i]==word[0]){
                int len=0;
                for(int j=0;i+j<sequence.size();j++){
                    if(sequence[i+j]==word[j%(word.size())]) len++;
                    else break;
                }
                //cout<<i<<" "<<len<<endl;
                res=max(res,int(len/(word.size())));
            }
        }
        return res;
    }
};

标签:word,sequence,重复,重复子,int,1668,字符串
From: https://www.cnblogs.com/SkyDusty/p/16853567.html

相关文章