#K54822. Minimum Rotations to Transform String
Minimum Rotations to Transform String
Minimum Rotations to Transform String
You are given two strings S and T of length n. In a single operation, you can remove the first character of S and append it to the end of S (a rotation). Your task is to determine the minimum number of such operations required to transform S into T. If it is impossible to transform S into T using these operations, output -1.
More formally, find the minimum non-negative integer i such that
where S[i:]
denotes the substring of S starting from the i-th character (0-indexed) and S[:i]
denotes the substring of S before the i-th character. If no such i exists, print -1.
Note: It is necessary that both strings have the same multiset of characters for a valid transformation to exist.
inputFormat
The input consists of three lines:
- The first line contains an integer n representing the length of the strings.
- The second line contains the string S.
- The third line contains the string T.
outputFormat
Output a single integer which is the minimum number of operations required to transform S into T. If it is impossible, output -1.
## sample5
abcde
cdeab
2