Last Updated: 20 Aug, 2025

Repeated String Match

Moderate
Asked in company
Amazon

Problem statement

You are given two strings, 'a' and 'b'.

Your task is to find the minimum number of times you need to repeat string 'a' such that string 'b' becomes a substring of the resulting string. If it's impossible for 'b' to be a substring of a repeated 'a', you should return -1.

For example, repeating "abc" 3 times results in the string "abcabcabc".


Input Format:
The first line of input contains the string 'a'.

The second line of input contains the string 'b'.


Output Format:
Print a single integer representing the minimum number of repetitions of 'a' required, or -1 if it's impossible.


Note:
Repeating a string 0 times results in an empty string.

The strings 'a' and 'b' consist of lowercase English letters.