LeetCode 686, Medium. Topics: String, String Matching, Z Algorithm, Knuth-Morris-Pratt Algorithm, Boyer-Moore String-Search Algorithm. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 24 parametrized pytest cases, and a playground notebook:
Problem
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".
Examples
Constraints
- 1 <= a.length, b.length <= 10^4
- a and b consist of lowercase English letters.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026