Open In App

How to Check if One String is a Rotation of Another in Java?

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, to check if one string is a rotation of another, we can do string concatenation and substring matching. A rotated string is created by moving some characters from the beginning of the string to the end while maintaining the character order.

Program to Check if One String is a Rotation of Another in Java

One simple solution is to concatenate the first string with itself. If the second string is a substring of this concatenated string, then it is a valid rotation. This approach uses the contains() method in Java for substring checking.

Example:

Java
// Java program to check if two given 
// strings are rotations of each other
public class StringRotation {

    // Method to check if two strings 
    // are rotations of each other
    public static boolean areRotations(String s1, String s2) {
      
        // Check if lengths are equal and s2 is a 
        // substring of s1 concatenated with s1
        return s1.length() == s2.length() && (s1 + s1).contains(s2);
    }

    public static void main(String[] args) {
      
        // Input strings
        String s1 = "ABCD";
        String s2 = "CDAB";

        // Check if s2 is a rotation of s1
        if (areRotations(s1, s2)) {
            System.out.println("\"" + s2 + "\" is a rotation of \"" 
                               + s1 + "\".");
        } else {
            System.out.println("\"" + s2 + "\" is not a rotation of \"" 
                               + s1 + "\".");
        }

        // Additional test cases
        String s3 = "ABAD";
        String s4 = "ADAB";
        if (areRotations(s3, s4)) {
            System.out.println("\"" + s4 + "\" is a rotation of \"" 
                               + s3 + "\".");
        } else {
            System.out.println("\"" + s4 + "\" is not a rotation of \"" 
                               + s3 + "\".");
        }

        String s5 = "ABCD";
        String s6 = "ACBD";
        if (areRotations(s5, s6)) {
            System.out.println("\"" + s6 + "\" is a rotation of \"" 
                               + s5 + "\".");
        } else {
            System.out.println("\"" + s6 + "\" is not a rotation of \"" 
                               + s5 + "\".");
        }
    }
}

Output
"CDAB" is a rotation of "ABCD".
"ADAB" is a rotation of "ABAD".
"ACBD" is not a rotation of "ABCD".

Explanation: This example compares both string length and ensures the second string appears in the concatenated first string.


Next Article

Similar Reads