How to check if String is rotation of another in java

In this post, we will check if one String is rotation of another.
For example:
code2master and mastercode2 are rotation of each other but code2master and master2code are not.

Algorithm:
The algorithm is very simple.

  • Create new String str3 by contantinating str1 twice
  • If Str3 contains str2 then str1 and str2 are rotation of each other

Java program to find if String is rotation of another.

Output:

code2master and mastercode2 are rotation of each other:true
code2master and master2code are rotation of each other:false

That’s all about checking if String is rotation of another.

Leave a Reply

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