Check if One String is Rotation of Other in Java [2 ways]

In this post, I will be sharing how to check if one string is the rotation of the other in Java with examples. There are 2 ways to achieve our goal:

1. Using String Concatenation [Easiest]

2. Using StringBuilder

Read Also: Difference between String, StringBuilder, and StringBuffer in Java

What does the rotation of the String mean?

A string is considered as a rotation of another string if it has the same length, contains the same characters, and is rotated around one of the characters. First, we will understand the question with the help of the examples:

If the str1 and str2 are two input strings as shown below:
str1 = "AliveIsAwesome" 
str2 = "someAliveIsAwe"
Then it should print true as output since str2 is a rotated string of str1.

str1 = "AliveIsAwesome" 
str2 = "IsAwesomeAlive"
Then it should print true as output.

str1 = "AliveIsAwesome" 
str2 = "AwesomeIsAlive"
Then it should print false as output

Check if One String is Rotation of Other in Java

1. Using String Concatenation


This is the easiest way to check if one string is the rotation of another string. Follow the below steps:

Step 1: Check if two strings str1 and str2 are equal in length. If they differ in length then str2 is not a rotated version of str1.

Step 2: If two strings str1 and str2 are of the same length then just create another string str3 by concatenating the first string str1 with itself as shown below:

str3 = str1 + str1

str1 = "AliveIsAwesome" 
str3 = "AliveIsAwesomeAliveIsAwesome"

Step 3: Check whether string str3 contains string str2 using contains() method of the String class. If yes, then the second string is a rotation of the first.

public class CheckStringRotation
{
    public static void main(String args[])
    {
        String str1 = "AliveIsAwesome";
        String str2 = "IsAwesomeAlive";
        //Step 1 
        if(str1.length() == str2.length())
        {
           //Step 2
           String str3 = str1 + str1; 
           //Step 3
           boolean result = str3.contains(str2);
           if(result == true)
             System.out.println("str2 is rotated version of str1");
           else
             System.out.println("str2 is NOT rotated version of str1");
        }
        else
        {
            System.out.println("str2 is NOT rotated version of str1 because they differ in length");
        }
        
    }
}

Output:
str2 is rotated version of str1


2. Using StringBuilder


This is another way to check if one string is the rotation of another string. Follow the below steps:

Step 1: Verify if two strings str1 and str2 are equal in length. If yes, then proceed to step 2.

Step 2: Convert the given string str1 to StringBuilder by using the StringBuilder class constructor that accepts String str1. Just like above, instead of doing string concatenation, we will append the string str1 to the StringBuilder object, since StringBuilder is mutable.

Step 3: Convert the StringBuilder object to String using the toString() method and then check whether the string contains str2 or not.

public class CheckStringRotation2
{
    public static void main(String args[])
    {
        String str1 = "BeInPresent";
        String str2 = "PresentInBe";
        //Step 1 
        if(str1.length() == str2.length())
        {
           //Step 2
           StringBuilder sb = new StringBuilder(str1);
           sb.append(str1);
           //Step 3
           boolean result = sb.toString().contains(str2);
           if(result)
             System.out.println("str2 is rotated string of str1");
           else
             System.out.println("str2 is NOT rotated string of str1");
        }
        else
        {
            System.out.println("str2 is NOT rotated string of str1 because they differ in length");
        }
        
    }
}

Output:
str2 is NOT rotated string of str1


That's all for today. Please mention in the comments if you have any questions related to how to check if one string is the rotation of another string in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry