{x}
blog image

Repeated String Match

Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b​​​​​​ to be a substring of a after repeating it, return -1.

Notice: string "abc" repeated 0 times is "", repeated 1 time is "abc" and repeated 2 times is "abcabc".

 

Example 1:

Input: a = "abcd", b = "cdabcdab"
Output: 3
Explanation: We return 3 because by repeating a three times "abcdabcdabcd", b is a substring of it.

Example 2:

Input: a = "a", b = "aa"
Output: 2

 

Constraints:

  • 1 <= a.length, b.length <= 104
  • a and b consist of lowercase English letters.

Solution Explanation for Repeated String Match

This problem asks to find the minimum number of times string a needs to be repeated to contain string b as a substring. If it's impossible, return -1.

Approach

The core idea is to iteratively repeat string a and check if b is a substring. We can optimize this by making an educated guess about the minimum repetitions needed. Since b must fit within the repeated a, a reasonable starting point is ceil(len(b) / len(a)). We then check if b is a substring within this repeated string. If not, we increment the repetition count and check again, repeating this process a few times to account for edge cases where b might only appear after several repetitions. If b is not found within a reasonable number of repetitions, we conclude that it's not possible and return -1.

Time and Space Complexity Analysis

  • Time Complexity: O(m * n), where m is the length of string a and n is the length of string b. This is because in the worst case, we might concatenate a up to n/m + 3 times (a constant factor increase), and the contains (or equivalent) operation within the loop takes O(m*n) in the worst case. However, the practical time complexity is often significantly lower.

  • Space Complexity: O(m*n) in the worst case, because we might build a string of length up to O(m * n) when repeating a. Again, the practical space complexity is usually far less than this upper bound.

Code Explanation (Python)

class Solution:
    def repeatedStringMatch(self, a: str, b: str) -> int:
        m, n = len(a), len(b)
        ans = math.ceil(n / m) #initial guess of repetitions
        t = a * ans  # Repeat a, ans times
 
        for _ in range(3): # Try up to 3 additional repetitions
            if b in t:   #Efficient substring check using 'in' operator
                return ans
            ans += 1
            t += a      #Append a to t
 
        return -1 # b not found within a reasonable number of repetitions

The Python code efficiently uses the in operator for substring checking and string multiplication for repeating a. The loop iterates a few times for robustness and handles edge cases effectively. The math.ceil function ensures we always have enough repetitions initially.

Code Explanation (Other Languages)

The Java, C++, Go, and TypeScript solutions follow a similar logic to the Python solution. They differ slightly in syntax but accomplish the same task. For instance, Java uses StringBuilder for efficient string manipulation, while C++ uses string's find method and += operator. Go leverages strings.Repeat and strings.Contains. TypeScript's implementation closely mirrors the Python version. The key concept is the iterative repetition and substring check, along with a limited number of iterations to handle edge cases and avoid infinite loops.