Leetcode第1668题:最大重复子字符串(Maximum repeating subarray)

解题思路

题目条件字符串长度不超过100,直接从大到小枚举word的重复次数k,判断word重复次数后是否还是sequence的字串,是就直接返回重复次数k
核心代码如下:

class Solution {
public:
    int maxRepeating(string sequence, string word) {
        int ans = 0;
        string t = word;
        int x = sequence.size() / word.size();    //最大包含次数
        for (int k = 1; k <= x; ++k) {
            // 这里从小到大枚举重复值
            if (sequence.find(t) != string::npos) {
                ans = k;
            }
            t += word;
        }
        return ans;
    }
};
posted @ 2022-11-03 12:18  hql5  阅读(29)  评论(0)    收藏  举报