How to check if strings are rotations of each other in Java

1 Answer

0 votes
public class MyClass {
    static boolean rotations(String str1, String str2) {
        if (str1.length() != str2.length())
            return false;

        System.out.println(str1 + str1);
        
        return (str1 + str1).indexOf(str2) != -1;
    }
    public static void main(String args[]) {
        String str1 = "abcxd", str2 = "cxdab";
 
        if (rotations(str1, str2))
            System.out.println("yes");
        else
            System.out.printf("no");
    }
}




/*
run:
  
abcxdabcxd
yes

*/

 



answered Dec 23, 2021 by avibootz

Related questions

1 answer 176 views
1 answer 124 views
1 answer 191 views
1 answer 98 views
1 answer 161 views
...