You’re given a string 'S' of length 'N'. Your task is to find the minimum number of rotations to get the same string 'S'.
NOTE :The string only consists of lowercase English alphabets.
The first line of input contains 'T', the number of test cases.
The first line of each test case contains an integer 'N', denoting the length of string 'S'.
The second line of each test case contains the string 'S'.
Output Format :
The only line of output of each test case should contain an integer denoting the minimum rotations required to get the same string 'S'.
Print the output of each test case in a separate line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10 ^ 4
Time Limit: 1 sec
The string 'S' only contains lowercase English characters.
Where 'T' denotes the number of test cases and 'N' denotes the length of string 'S'.
1
5
ninja
5
After five rotations we get the same string i.e “ninja”.
1
5
ccccc
1
Just after one rotation, we get the same string i.e “ccccc”.
Try to generate all possible strings starting from each index.
The idea that we will be using in this approach is that we start from position 1 and start generating all substrings of length N and check if this substring is equal to the original string S or not. Let’s see how we will achieve this.
For Example: N = 3 and S = abc
O(N^2), where N is the length of the string S.
There are N characters and for each character, we’ll be doing N operations Hence, the overall Time Complexity is O(N^2).
O(1).
We are using constant extra memory. Hence, the overall Time Complexity is O(1).