![]() |
VOOZH | about |
Given a string str and an integer K, the task is to check whether the given string is K-periodic. A string is k-periodic if the string is a repetition of the sub-string str[0 ... k-1] i.e. the string "ababab" is 2-periodic. Print Yes if the given string is k-periodic, else print No.
Examples:
Input: str = "geeksgeeks", k = 5
Output: Yes
Given string can be generated by repeating the prefix of length k i.e. "geeks"
Input: str = "geeksforgeeks", k = 3
Output: No
Approach: Starting with the sub-string str[k, 2k-1], str[2k, 3k-1] and so on, check whether all of these sub-strings are equal to the prefix of the string of length k i.e. str[0, k-1]. If the condition is true for all such sub-strings, then print Yes else print No.
Below is the implementation of the above approach:
Yes
Time Complexity: O(K * log(len))
Auxiliary Space: O(1)