How to check if one String is rotation of another String in java

In this post, we will see how to check if one String is rotation of another or not.

For example:

Approach:

Lets say you need to check whether str1 and str2 is rotation of one another or not.

  1. Create a new String with str3= str1 + str1
  2. Check if str3 contains str2 or not.
  3. if str3 contains str2 then str2 is rotation of str1 else it is not

Java Program to check if one String is rotation of another.

When you run above program, you will get below output:

java2blog and blogjava2 are rotation of each other : true
java2blog and avablog2j are rotation of each other : false

That’s all about how to check if one String is rotation of another String in java

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *