Java Program to Check if Two Strings are Cyclic Permutations of Each Other

This is the Java Program to Determine if 2 Strings are Cyclic Permutations of Each Other.

Problem Description

Given two different strings of the same length, find out if they are cyclic permutations of each other, that is one string can be transformed into another, by cyclically rotating its elements.

Example:
String str =”ABCD”;
String str2 = “CDAB”;

Output = “Yes”

Problem Solution

Concatenate any one of the string to itself, let the concatenated string be str3. Now, check whether the other string is a substring of str3, if it is then str and str2 are cyclic permutations of one another, otherwise they aren’t.

Program/Source Code

Here is the source code of the Java Program to Determine if 2 Strings are Cyclic Permutations of Each Other. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
advertisement
  1.  
  2. // Java Program to Determine if 2 Strings are Cyclic Permutations of Each Other
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. public class StringRotation {
  9.     // Function to read input and display the output
  10.     public static void main(String[] args) {
  11.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12.         String str;
  13.         System.out.println("Enter the string");
  14.         try {
  15.             str = br.readLine();
  16.         } catch (Exception e) {
  17.             System.out.println("An I/O error occurred");
  18.             return;
  19.         }
  20.         String str2;
  21.         System.out.println("Enter the second string");
  22.         try {
  23.             str2 = br.readLine();
  24.         } catch (Exception e) {
  25.             System.out.println("An I/O error occurred");
  26.             return;
  27.         }
  28.         if(str.length() != str2.length())
  29.         {
  30.            System.out.println("\nNo");
  31.            return;
  32.         }
  33.         String str1=str;
  34.         str1 += str;
  35.         if(str1.indexOf(str2)!=-1)
  36.         {
  37.             System.out.println("\nYes");
  38.         }
  39.         else{
  40.             System.out.println("\nNo");
  41.         }
  42.     }
  43. }
Program Explanation

1. In main() function, firstly both the strings are entered.
2. Then the lengths of the two string are compared if they are unequal then No is displayed.
3. Otherwise, the first string is concatenated with itself and stored in the variable in str1.
4. Now, it is checked whether the second string is a subset of str1 using the command str1.indexOf(str2).
5. If the condition is true, then Yes is displayed, otherwise, No is displayed.

Time Complexity: O(1).

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
Case 1 (Positive Test Case):
 
Enter the string
ABCD
Enter the second string
CDAB
 
Yes
 
Case 2 (Negative Test Case):
 
Enter the string
ABCD
Enter the second string
DCAB
 
No
 
Case 3 (Positive Test Case - another example):
 
Enter the string
XYZ
Enter the second string
ZXY
 
Yes

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.