Suppose we have two strings s and t, we have to find how many times the string s can be concatenated to generate t. If we cannot generate t using s, then return -1.
So, if the input is like s = "tom" t = "tomtomtom", then the output will be 3 as we can concatenate "tom" 3 times to get "tomtomtom".
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(s, t): if(len(t) % len(s) != 0): return -1; cnt = int(len(t) / len(s)) s = s * cnt if(s == t): return cnt return -1 s = "tom" t = "tomtomtom" print(solve(s, t))