Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A string S = S[0]S[1] . . .S[N-1] consisting of N characters is given. The K-th cyclic
- shift of this string, where 0 <= K < N, is a string T defined as follows:
- T = S[K]S[K+1]...S[N-1]S[O]S[1]...S[K-1], for 0 < K < N,
- T = S, for K=0.
- For example, these are all cyclic shifts of the string S = "abcdefgh":
- - "abcdefgh" is 0th cyclic shift of "abcdefgh"
- - "bcdefgha" is 1st cyclic shift of "abcdefgh"
- - "cdefghab" is 2nd cyclic shift of "abcdefgh"
- - "defghabc" is 3rd cyclic shift of "abcdefgh"
- - "efghabcd" is 4th cyclic shift of "abcdefgh"
- - "fghabcde" is Sth cyclic shift of "abcdefgh"
- - "ghabcdef" is 6th cyclic shift of "abcdefgh"
- - "habcdefg" is 7th cyclic shift of "abcdefgh"
- If a cyclic shift of a given sting S is equal to S, then it is called a cyclic
- 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:
- 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).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement