Leetcode 686: Repeated String Match

grid47
grid47
Exploring patterns and algorithms
Aug 30, 2024 5 min read

A string repeated multiple times, with the matching part glowing softly as it’s found.
Solution to LeetCode 686: Repeated String Match Problem

You are given two strings, a and b. You need to find the minimum number of times you should repeat string a so that string b becomes a substring of the repeated a. If it is impossible, return -1.
Problem
Approach
Steps
Complexity
Input: You are given two strings `a` and `b` consisting of lowercase English letters.
Example: a = 'xyz', b = 'yzxyz'
Constraints:
• 1 <= a.length, b.length <= 10^4
• a and b consist of lowercase English letters.
Output: Return the minimum number of times string `a` should be repeated such that string `b` becomes a substring of it. If not possible, return -1.
Example: For a = 'xyz' and b = 'yzxyz', the output is 2.
Constraints:
• The output should be an integer representing the minimum number of times string `a` should be repeated.
Goal: To find the minimum number of repetitions of string `a` that contains string `b` as a substring.
Steps:
• 1. Start by iterating over string `a` and try matching its substring with string `b`.
• 2. If the substring of repeated `a` matches `b`, return the number of repetitions.
• 3. If no such match is found, return -1.
Goal: The problem has constraints ensuring the strings are manageable in size and consist of lowercase letters.
Steps:
• The length of both strings `a` and `b` are between 1 and 10^4.
• Both strings `a` and `b` consist of lowercase English letters.
Assumptions:
• Both strings are not empty and contain only lowercase letters.
Input: a = 'xyz', b = 'yzxyz'
Explanation: Repeating string `a` two times gives 'xyzxyz', which contains 'yzxyz' as a substring.

Input: a = 'ab', b = 'ababab'
Explanation: Repeating string `a` three times gives 'ababab', which is exactly string `b`.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus