A non-empty string S = S[0]S[1]...S[N-1] consisting of N characters is given. TheK-th cyclic shift of this string, where 0 ≤ K < N, is a string T defined as follows:
For example, these are all cyclic shifts of the string S = "codility": "codility" is 0th cyclic shift of "codility" "odilityc" is 1st cyclic shift of "codility" "dilityco" is 2nd cyclic shift of "codility" "ilitycod" is 3rd cyclic shift of "codility" "litycodi" is 4th cyclic shift of "codility" "itycodil" is 5th cyclic shift of "codility" "tycodili" is 6th cyclic shift of "codility" "ycodilit" is 7th cyclic shift of "codility" If a cyclic shift of a given string S is equal to S, then it is called acyclic automorphism of S. Each string has at least one cyclic automorphism, namely the 0th cyclic shift. For example, the 0th and 3rd cyclic shifts of the string "byebye" constitute all of its cyclic automorphisms. Write a function: class Solution { public int solution(String S); } that, given a string S of length N, returns the number of cyclic automorphisms of S. For example, given S = "byebye", the function should return 2, as explained in the example above. Assume that: · N is an integer within the range [1..1,000,000]; · string S consists only of lower-case letters (a−z). Complexity: · expected worst-case time complexity is O(N); · expected worst-case space complexity is O(N) (not counting the storage required for input arguments). // you can also use imports, for example: // import java.math.*; class Solution { public int solution(String S) { // write your code in Java SE 6 } }
Refer: https://github.com/monkeylyf/interviewjam/blob/master/str/Max_Number_of_Cyclic_Automorphism.java KMP : http://billhoo.blog.51cto.com/2337751/411486 |