Max_Number_of_Cyclic_Automorphism

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:


T = S[K]S[K+1]...S[N−1]S[0]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 = "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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值