## 466. Count The Repetitions

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


b'
\n\n

## Solution

\n
\n

#### Approach #1 Brute force [Time Limit Exceeded]

\n

Intuition

\n

According to the question, we need to find such that is the largest subsequence that can be found in . is essentially and is and so, we can find the number of times repeats in , say . And the number of times repeats in is therefore . Simple.

\n

Algorithm

\n
\n
• Initialize and . represents the current index in to be checked against and represents the number of times repeats in .
• \n
• Iterate over the variable from to :
\n
• Iterate over the variable from to :
\n
• If is equal to , increment .
• \n
• If is equal to , this implies that has completed one repartition and hence set and increment the .
• \n
\n
• \n
\n
• \n
• Return since, is .
• \n
\n\n

Complexity Analysis

\n
\n
• \n

Time complexity: .

\n
\n
• We iterate over the entire length of string for times.
• \n
\n
• \n
• \n

Space complexity: extra space for and .

\n
• \n
\n
\n

#### Approach #2 A better brute force [Accepted]

\n

Intuition

\n

In Approach #1, we simply checked for repetition over the entire . However, could be quiet large and thus, is inefficient to iterate over complete . We can take advantage of the fact that is repeating and hence, we could find a pattern of repetition of in . Once, we get the repetition pattern, we can easy calculate how many times the pattern repeats in in .

\n

But what\'s the pattern!

\n

In approach #1, we kept which tells the index to search in . We try to see in the below illustration if this repeats itself after some fixed iterations of or not and if so, then how can we leverage it.

\n \n

After finding the repitition pattern, we can calculate the sum of repeating pattern, part before repitition and part left after repitition as the result in .

\n

But will this repitition always take place?

\n

Yes! By Pigeonhole principle, which states that if items are put into containers, with , then at least one container must contain more than one item. So, according to this, we are sure to find 2 same after scanning at max blocks of .

\n

Algorithm

\n
\n
• Intialize nd , which are same as in Approach #1.
• \n
• Initialize 2 arrays, say and of size , initialized with 0. The size is based on the Pigeonhole principle as discussed above. The 2 arrays specifies the and at the start of each block.
• \n
• Iterate over from to :
\n
• Iterate over from to :
\n
• If , increment .
• \n
• If is equal to , set and increment .
• \n
\n
• \n
• Set and \n
• \n
• \n

Iterate over from to :

\n
\n
• If we find the repitition, i.e. current , we calculate the count for block before the repitition starts, the repeating block and the block left after repitition pattern, which can be calculated as:
• \n
\n

\n\n

\n
\n
• Sum the 3 counts and return the sum divided by , since \n
• \n
• If no repetition is found, return .
• \n
\n
• \n
\n
• \n
\n\n

Complexity analysis

\n
\n
• \n

Time complexity: .

\n
\n
• According to the Pigeonhole principle, we need to iterate over only times at max.
• \n
\n
• \n
• \n

Space complexity: extra space for and string.

\n
• \n
\n
\n

Analysis written by @abhinavbansal0.

\n
'