
First-line contains ‘T’, denoting the number of Test cases.
For each Test case:
The first line contains an integer, ‘N’, denoting the length of the input string S.
The following line contains the string ‘S’ of length ‘N’.
For each test case, you have to print an integer denoting the minimum number of operations required to form the given string ‘S’.
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
Note- Sum of ‘N’ over all test cases does not exceed 10^5.
Time Limit: 1 sec
Algorithm:
Approach: Let’s say ‘X’ is the answer for a string ‘S’ of length ‘N’, then the length of the string ‘T’, let’s say ‘Y’ will be (N / X). Also, S[0…(N - 1 - Y)] will be equal to S[Y….(N - 1)] as S = ‘T’ * X (i.e. the string ‘T’ is repeated ‘X’ times so the first (X - 1) * ‘Y’ characters will be equal to the last (X - 1) * ‘Y’ characters). So we have to find the largest suffix of the string ‘S’, which is also a prefix of the string ‘S’. We can find this using the Z - Algorithm. Then we can find ‘Y’ and then the ‘X’ using some basic algebra.
Algorithm: