Check if a string is rotation of another WITHOUT concatenating

后端 未结 11 1383
死守一世寂寞
死守一世寂寞 2020-12-25 08:11

There are 2 strings , how can we check if one is a rotated version of another ?

For Example : hello --- lohel

One simple solution is by co

相关标签:
11条回答
  • 2020-12-25 08:40

    You could compute the lexicographically minimal string rotation of each string and then test if they were equal.

    Computing the minimal rotation is O(n).

    This would be good if you had lots of strings to test as the minimal rotation could be applied as a preprocessing step and then you could use a standard hash table to store the rotated strings.

    0 讨论(0)
  • 2020-12-25 08:44

    Another python implementation (without concatenation) although not efficient but it's O(n), looking forward for comments if any.

    Assume that there are two strings s1 and s2.

    Obviously, if s1 and s2 are rotations, there exists two sub strings of s2 in s1, the sum of them will total to the length of the string.

    The question is to find that partition for which I increment an index in s2 whenever a char of s2 matches with that of s1.

    def is_rotation(s1, s2):
        if len(s1) != len(s2):
            return False
        n = len(s1)
        if n == 0: return True
    
        j = 0
        for i in range(n):
            if s2[j] == s1[i]:
                j += 1
        return (j > 0 and s1[:n - j] == s2[j:] and s1[n - j:] == s2[:j])
    

    The second and condition is just to ensure that the counter incremented for s2 are a sub string match.

    0 讨论(0)
  • 2020-12-25 08:48

    Trivial O(min(n,m)^2) algorithm: (n - length of S1, m - length of S2)

    isRotated(S1 , S2):

    if (S1.length != S2.length)
        return false
    for i : 0 to n-1
        res = true
        index = i
        for j : 0 to n-1
           if S1[j] != S2[index]
               res = false
               break
           index = (index+1)%n
        if res == true
            return true
    return false
    

    EDIT:

    Explanation -

    Two strings S1 and S2 of lengths m and n respectively are cyclic identical if and only if m == n and exist index 0 <= j <= n-1 such S1 = S[j]S[j+1]...S[n-1]S[0]...S[j-1].

    So in the above algorithm we check if the length is equal and if exist such an index.

    0 讨论(0)
  • 2020-12-25 08:49

    One simple solution is by concatenating them and checking if the other one is a substring of the concatenated version.

    I assume you mean concatenate the first string with itself, then check if the other one is a substring of that concatenation.

    That will work, and in fact can be done without any concatenation at all. Just use any string searching algorithm to search for the second string in the first, and when you reach the end, loop back to the beginning.

    For instance, using Boyer-Moore the overall algorithm would be O(n).

    0 讨论(0)
  • 2020-12-25 08:55
        String source = "avaraavar";
        String dest =   "ravaraava";
    
        System.out.println();
        if(source.length()!=dest.length())
            try {
                throw (new IOException());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        int i = 0;
        int j = 0;
        int totalcount=0;
        while(true)
        {
            i=i%source.length();    
            if(source.charAt(i)==dest.charAt(j))
            {   
                System.out.println("i="+i+" , j = "+j);
                System.out.println(source.charAt(i)+"=="+dest.charAt(j));
                i++;
                j++;
                totalcount++;   
            }
            else
            {
                System.out.println("i="+i+" , j = "+j);
                System.out.println(source.charAt(i)+"!="+dest.charAt(j));
                i++;
                totalcount++;   
                j=0;
            }
            if(j==source.length())
            {
                System.out.println("Yes its a rotation");
                break;
            }
            if(totalcount >(2*source.length())-1)
            {
                System.out.println("No its a rotation");
                break;
            }
        }
    
    0 讨论(0)
  • 2020-12-25 08:56

    A very straightforward solution is to rotate one of the words n times, where n is the length of the word. For each of those rotations, check to see if the result is the same as the other word.

    0 讨论(0)
提交回复
热议问题