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.
- Create a new String with
str3
=str1
+str1
- Check if
str3
containsstr2
or not. - if
str3
containsstr2
thenstr2
is rotation ofstr1
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
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?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.