Repeated String Match
Find the fewest copies of one string, joined end to end, that contain another string.
Given two strings abab
Example 1
Output: 3
Explanation: "abcdabcdabcd" contains "cdabcdab", but "abcdabcd" does not.
Example 2
Output: 2
The Idea
Picture a strip of paper printed with abcdabcdabcdabcd...cdabcdababcd
The word has 8 letters and each copy has 4, so two copies give exactly 8 letters, abcdabcdcdcd
The row labelled bcab
That raises the real question: how far might we have to go? Could some bb
Every copy is the same, so wherever babb
So only two strings need checking: q copies and q + 1 copies. The q + 1 copies hold every possible start inside copy 1, so if b
Solution Approach
The simple way keeps adding copies until b
Adding copies until b appears
Why not just add a copy, search, and repeat? For Example 1 that works: one copy, two copies, then three, found. But for a = "abc"b = "wxyz"
The fewest copies worth checking
Which copy count should the first search use? Any string shorter than bab
The one extra copy as the limit
When should the search stop? q + 1 copies hold every possible start inside copy 1, so the limit is one more copy and one more search. A miss after that means the answer is -1. That gives exactly two searches, whatever the strings are.
Searching the joined string
How do we build and search the joined string? Java's String+StringBuilderindexOf(b)b
Java Solution
The method repeatedStringMatch
- First, create an empty
namedStringBuilder and a countersb set to 0. A loop appendstimes toa and adds 1 tosb whiletimes is shorter thansb . When it ends,b is q.times - Search
withsb . Ifsb.indexOf(b) is found, returnb .times - Otherwise append one more copy of
and search again. Ifa is found now, returnb .times + 1 - If neither search found
, return -1.b
The mainbbbaba
public class RepeatString {
public static int repeatedStringMatch(String a, String b) {
StringBuilder sb = new StringBuilder();
int times = 0;
while (sb.length() < b.length()) { // the fewest copies that are at least as long as b
sb.append(a);
times++;
}
if (sb.indexOf(b) >= 0) {
return times;
}
if (sb.append(a).indexOf(b) >= 0) { // one more copy covers any starting offset
return times + 1;
}
return -1;
}
public static void main(String[] args) {
System.out.println(repeatedStringMatch("abcd", "cdabcdab"));
System.out.println(repeatedStringMatch("a", "aa"));
System.out.println(repeatedStringMatch("abc", "b"));
System.out.println(repeatedStringMatch("abcd", "cda"));
System.out.println(repeatedStringMatch("abc", "acb"));
System.out.println(repeatedStringMatch("abc", "wxyz"));
}
}
Running it prints:
3
2
1
2
-1
-1
The 3 and the 2 at the top match the two examples. Example 1 needed the extra copy, and Example 2 did not: two copies of "a""aa"
The 1 on the third line is for b = "b""abc"b"cda""abcd""cda"a
The two -1 lines show the limit at work. "acb""abc""abcabc"ab"wxyz""abc"
The extra copy matters more than it looks. A version that checks only the first q copies returns -1 for both "cdabcdab""cda"
Complexity
The work is building at most q + 1 copies of aab
- Time: O(m × n), plus O(m + n) for the appends. The joined string holds fewer than n + 2m letters, so
can start at no more than 2m positions in it.b tries the starts one by one and compares up to n letters at each. WithindexOfKMP , a search that never steps back in the joined string, the time drops to O(m + n). - Space: O(m + n) for the joined string in
, which never holds more than n + 2m letters.sb
A related trick, joining a string to itself to see all its rotations, solves