Ex: - "code" "odec" ( rotating "code" 1 time left gives "odec" )
Algorithm : To check for "code" and "deco"
- Choose a string and concatenate it to itself ( for ex:- here , we choose "deco" you can choose "code" also.) ==> ("deco" + "deco" =="decodeco")
- Now take another string and check whether it is present in the string generated in step 1. ( 2nd string is "code" we have to check whether "code" is present in "decodeco". if present print "YES" otherwise "NO". here "code" is present in "decodeco" so the answer is "YES".
- The same approach is followed to solve this question See below.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Scanner; public class CheckTwoStringRotation { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String string1 = sc.next(); String string2 = sc.next(); String temp = string2 + string2; if (temp.contains(string1)) System.out.println("Yes"); else System.out.println("No"); } } |
Python
input_str1 = input("Enter first string:") input_str2 = input("Enter second string:") if input_str1 in input_str2*2: print("YES") else: print("NO") |