Check if a string is rotation of another WITHOUT concatenating

后端 未结 11 1382
死守一世寂寞
死守一世寂寞 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:59

    input1= "hello" input2="llohe" input3="lohel"(input3 is special case)

    if length's of input 1 & input2 are not same return 0.Let i and j be two indexes pointing to input1 and input2 respectively and initialize count to input1.length. Have a flag called isRotated which is set to false

    while(count != 0){

    When the character's of input1 matches input2

    1. increment i & j
    2. decrement count

    If the character's donot match

    1. if isRotated = true(it means even after rotation there's mismatch) so break;
    2. else Reset j to 0 as there's a mismatch. Eg:

    Please find the code below and let me know if it fails for some other combination I may not have considered.

        public boolean isRotation(String input1, String input2) {
        boolean isRotated = false;
        int i = 0, j = 0, count = input1.length();
    
        if (input1.length() != input2.length())
            return false;
    
        while (count != 0) {
    
            if (i == input1.length() && !isRotated) {
                isRotated = true;
                i = 0;
            }
    
            if (input1.charAt(i) == input2.charAt(j)) {
                i++;
                j++;
                count--;
            }
    
            else {
                if (isRotated) {
                    break;
                }
                if (i == input1.length() - 1 && !isRotated) {
                    isRotated = true;
                }
                if (i < input1.length()) {
                    j = 0;
                    count = input1.length();
    
                }
                /* To handle the duplicates. This is the special case.
                 * This occurs when input1 contains two duplicate elements placed side-by-side as "ll" in "hello" while 
                 * they may not be side-by-side in input2 such as "lohel" but are still valid rotations. 
                 Eg: "hello" "lohel"
                */
                if (input1.charAt(i) == input2.charAt(j)) {
                    i--;
                }
                i++;
            }
        }
        if (count == 0)
            return true;
        return false;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(new StringRotation().isRotation("harry potter",
                "terharry pot"));
        System.out.println(new StringRotation().isRotation("hello", "llohe"));
        System.out.println(new StringRotation().isRotation("hello", "lohell"));
        System.out.println(new StringRotation().isRotation("hello", "hello"));
        System.out.println(new StringRotation().isRotation("hello", "lohe"));
    }
    

提交回复
热议问题