Given a string ‘S’ find whether the given string is periodic or not.
A string is said to be periodic if it repeats itself after a certain number of characters and the period of repetition is less than the size of the string.
For example: Let ‘S’ be “ababab” we can clearly see that this string is periodic as ‘ab’ is repeated 3 times to make the string ‘S’.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.
The first line of each test case contains a string ‘S’ for which we need to find if a given string is periodic or not
Output Format
For each test case, return true if the given string is periodic and return false if the given string is not periodic.
Output for each test case must be in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1<= S.length<=10^5
Where 'S.length' denotes the length of string ‘S’.
The given string consists of lowercase English alphabets only.
Time limit: 1 sec
3
xxxxxx
aabbaaabba
abcba
True
True
False
Test Case 1:
In the first test case, we can clearly see that the string has a repeating string ‘x’ 6 times. So we return true
Test Case 2:
In the second test case, we can see that the string ‘aabba’ repeats twice to form the given string. Hence we return true,
Test Case 3:
In the third test case, we can see that there is no string which repeats itself to form the given string, hence we return false.
2
vwvnqpnchqik
ubzumubzumubzumubzum
False
True
SImply brute force the solution
Keeping the above example in mind, we can write the following solution:
O(N), Where ‘N’ size of the string
We need to check for equality of string and also concatenate string which takes O(N) time.
O(N), where ‘N’ is the size of the string.
We need to make a new string to compare it with the older one which takes o(N) space.