A signals-intelligence analyst has intercepted a long transmission string and suspects it hides a coded passphrase, repeated back-to-back some number of times as camouflage. For a positive integer k, the k-fold echo of the passphrase is the passphrase concatenated with itself k times, with no characters in between (for example, the 3-fold echo of "ab" is "ababab"). The analyst wants to know the greatest k for which the k-fold echo occurs somewhere inside the transmission as one unbroken block. If not even a single copy of the passphrase (k = 1) appears anywhere in the transmission, the answer is 0.
The first line contains the transmission string. The second line contains the passphrase string. Both strings consist only of lowercase English letters and contain no spaces.
Print a single integer: the largest k such that the passphrase repeated k times back-to-back appears as a contiguous block inside the transmission, or 0 if no such k >= 1 exists.
Example 1
Input
ababab ab
Expected
3
Explanation
The passphrase "ab" repeated 3 times back-to-back forms "ababab", which is exactly the transmission, so it fits as a contiguous block. Repeating it a 4th time would need 8 characters, longer than the whole transmission, so 3 is the largest depth that fits.
Example 2
Input
xyzxyzxamn xyz
Expected
2
Explanation
"xyz" repeated twice forms "xyzxyz", which appears at the very start of the transmission. Repeating it a third time would need "xyzxyzxyz", but the transmission continues with "xamn" instead of a third "xyz", so 2 is the largest depth that fits anywhere in the transmission.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →