1. 程式人生 > 其它 >[LeetCode] 1668. 最大重複子字串

[LeetCode] 1668. 最大重複子字串

給你一個字串sequence,如果字串 word連續重複k次形成的字串是sequence的一個子字串,那麼單詞word 的 重複值為 k 。單詞 word的 最大重複值是單詞word在sequence中最大的重複值。如果word不是sequence的子串,那麼重複值k為 0 。

給你一個字串 sequence和 word,請你返回 最大重複值k 。

示例 1:

輸入:sequence = "ababc", word = "ab"
輸出:2
解釋:"abab" 是 "ababc" 的子字串。
示例 2:

輸入:sequence = "ababc", word = "ba"
輸出:1
解釋:"ba" 是 "ababc" 的子字串,但 "baba" 不是 "ababc" 的子字串。
示例 3:

輸入:sequence = "ababc", word = "ac"
輸出:0
解釋:"ac" 不是 "ababc" 的子字串。

提示:

1 <= sequence.length <= 100
1 <= word.length <= 100
sequence 和word都只包含小寫英文字母。

    public int maxRepeating(String sequence, String word) {
        if (sequence == null || word == null)
            return 0;
        StringBuilder words 
= new StringBuilder(word); int res = 0; while (sequence.indexOf(words.toString()) >= 0) { res++; words.append(word); } return res; }