Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc". On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”. You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer M such that [S2,M] can be obtained from S1. Example: Input: s1="acb", n1=4 s2="ab", n2=2 Return: 2
Simple brute force solution: O(n1×len(s1) )
int getMaxRepetitions(string s1, int n1, string s2, int n2)
{
int len1 = s1.length(), len2 = s2.length();
int count1 = 0, count2 = 0, i = 0, j = 0;
while (count1 < n1)
{
if (s1[i] == s2[j])
{
if (++j == len2)
{
j = 0;
++count2;
}
}
if (++i == len1)
{
i = 0;
++count1;
}
}
return count2 / n2;
}
A great tutorial to this problem is found here:C++ solution inspired by @70664914 with organized explanation. It reuses if there is a pattern repeated.
Example1:
s1
= “abacb”,
n1
= 6,
s2
= “bcaa”,
n2
= 1.
variable | ababc | ababc | ababc | ababc | ababc | ababc |
---|---|---|---|---|---|---|
nextIndex | 2 | 1 | 2 | 1 | 2 | 1 |
count | 0 | 1 | 1 | 2 | 2 | 3 |
nextIndex is the first letter you are looking for in next
s1
segment.
count is the
s2
repeated count.
If nextIndex has a repeated pattern, then we could easily get the total
s2
repeated count.
Example2:
s1
= “cabacb”,
n1
= 6,
s2
= “bcaa”,
n2
= 1.
variable | cababc | cababc | cababc | cababc | cababc | cababc |
---|---|---|---|---|---|---|
nextIndex | 2 | 1 | 1 | 1 | 1 | 1 |
count | 0 | 1 | 2 | 3 | 4 | 5 |
Total repeat count = count before pattern + count in repeated pattern + count after repeated pattern
int getMaxRepetitions(string s1, int n1, string s2, int n2)
{
int len1 = s1.length(), len2 = s2.length();
vector<int> repeatCount(n1 + 1, 0);
unordered_map<int, int> nextIndex;
nextIndex[0] = 0;
int j = 0, count = 0;
for (int k = 1; k <= n1; ++k)
{
for (int i = 0; i < len1; ++i)
{
if (s1[i] == s2[j])
{
if (++j == len2)
{
j = 0;
++count;
}
}
}
repeatCount[k] = count;
if (nextIndex.count(j))
{
int start = nextIndex[j];
int prefixCount = repeatCount[start];
int patternCount = (repeatCount[k] - repeatCount[start]) * (n1 - start) / (k - start);
int suffixCount = repeatCount[start + (n1 - start) % (k - start)] - repeatCount[start];
return (prefixCount + patternCount + suffixCount) / n2;
}
nextIndex[j] = k;
}
return repeatCount[n1] / n2;
}