'How to find the minimum possible cycle shift to take from s1 to s2?

If I have 2 strings s1 and s2, where the s2 is a cyclically shifted string of the s1. It needs to find the minimum possible cycle shift to take from s1 to s2.

Let me show an example:

s1 = 'I love cookies '

s2 = 'cookies I love ' Here the answer is 7.

It is preferable to take in linear time. There is my failed trials:

def find_minimum_cyclic_shift(s1, s2):

        if len(s1) != len(s2):
            return -1

        index = s2.index(s1[0])
        if (index > -1):
            if (s1==s2):
                return 0;

            #finalPosition = len(s2) - index
            #print(finalPosition, " index=",index)
            #return s2[0] == s1[finalPosition] and s1[finalPosition::]==s2[0:index]
        return index

But it doesn't work for the case: absabsabsf and absfabsabs. Instead of 4 I have 0. because index function returns me only the first appearing of a letter.

Please, give me at least a logic to code it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source