Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 686 · String Matching (KMP / Rabin-Karp)

Repeated String Match

LeetCode 686 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Only ceil(|B|/|A|) or one more repeat can work.

ಮಧ್ಯಮStringString MatchingZ AlgorithmKnuth–Morris–Pratt AlgorithmBoyer–Moore String-Search Algorithm
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Repeated String Match (LeetCode 686) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — String Matching (KMP / Rabin-Karp) ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

B can only fit inside A repeated enough times to be at least as long as B — or one more copy, in case B starts partway through A. So try those two counts and check for B with a rolling-hash (Rabin–Karp) search.

Approach

  1. 1

    count = ceil(len(b) / len(a)); str = a repeated count times.

  2. 2

    If b is found in str, return count.

  3. 3

    Else try one more copy; otherwise -1.

  4. 4

    Rabin–Karp: compare a rolling character-sum hash, and verify characters on a hash hit.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Repeat + Rabin–KarpPythonO(n+m) expected
import math


class Solution:
    def repeatedStringMatch(self, a, b):
        count = math.ceil(len(b) / len(a))
        s = a * count
        if self.rabinKarp(s, b):
            return count
        elif self.rabinKarp(s + a, b):
            return count + 1
        else:
            return -1

    def rabinKarp(self, b, a):
        if len(a) > len(b):
            return False
        m = len(a)
        n = len(b)
        aHash = 0
        bHash = 0
        for i in range(m):
            aHash += ord(a[i])
            bHash += ord(b[i])
        for i in range(n - m + 1):
            if aHash == bHash:
                j = 0
                while j < m:
                    if a[j] != b[i + j]:
                        break
                    j += 1
                if j == m:
                    return True
            if i < n - m:
                bHash = bHash - ord(b[i]) + ord(b[i + m])
        return False
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Repeated String Match — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Repeated String Match ಹಿಂದಿನ intuition ಏನು?

B can only fit inside A repeated enough times to be at least as long as B — or one more copy, in case B starts partway through A. So try those two counts and check for B with a rolling-hash (Rabin–Karp) search.

Repeated String Match (LeetCode 686) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. count = ceil(len(b) / len(a)); str = a repeated count times. 2. If b is found in str, return count. 3. Else try one more copy; otherwise -1. 4. Rabin–Karp: compare a rolling character-sum hash, and verify characters on a hash hit.

Repeated String Match ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n + m) expected. Space: O(n). (Accepted approach: Repeat + Rabin–Karp.)

Repeated String Match ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

The additive hash collides often — the character check after a hash match is essential.