LeetCode題解(1668):最大重複子字串(Python)

長行發表於2020-12-08

題目:原題連結(簡單)

標籤:字串、雙指標

解法時間複雜度空間複雜度執行用時
Ans 1 (Python) O ( N 1 × N 2 ) O(N1×N2) O(N1×N2) O ( 1 ) O(1) O(1)36ms (76.94%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def maxRepeating(self, sequence: str, word: str) -> int:
        s1, s2 = len(sequence), len(word)

        ans = 0

        i1 = 0
        while i1 < s1:
            now, i2, i3 = 0, 0, i1
            while i3 < s1 and sequence[i3] == word[i2]:
                i2 += 1
                if i2 == s2:
                    now += 1
                    i2 = 0
                i3 += 1
            ans = max(ans, now)
            i1 += max(now - 1, 0) * s2 + 1

        return ans

相關文章