• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 13, 2022 |870 Views
Java Program to Check if Strings are Rotations of each other or not
Description
Discussion

In this video, we will see the Java Program to Check if Strings are Rotations of each other or not. 

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. 

Rules for String Rotations: 
Rule 1: Number of characters in both the strings must be equal. 
Rule 2: All characters must be the same. 
Rule 3: Rotated around one of the characters. i.e str2 must be a substring of the str1 concatenated with str1. 

Algorithm: 

1. First we create a temp string and store the concatenation of str1 to str1 in the temp string.i.e temp = str1.str1 
2. If str2 is a substring of temp then str1 and str2 are rotations of each other. 

Example: 
str1 = "abcd" 
str2 = "efgh" 
temp = str1.str1 = "abcdabcd" 
str2 = bcda 

Output: Yes
Since str2 is a substring of temp, str1 and str2 are rotations of each other. 

Here we use the string Concat method to check if Strings are Rotations of each other or not.

Java Program to Check if strings are rotation of each other or not: https://www.geeksforgeeks.org/java-program-to-check-if-strings-are-rotations-of-each-other-or-not/