Given two strings, the task is to find if a string can be obtained by rotating another string two places.
Input: string1 = “amazon”, string2 = “azonam”
// rotated anti-clockwise
Input: string1 = “amazon”, string2 = “onamaz”
// rotated clockwise
Asked in: Amazon Interview
1- There can be only two cases: a) Clockwise rotated b) Anti-clockwise rotated 2- If clockwise rotated that means elements are shifted in right. So, check if a substring[2.... len-1] of string2 when concatenated with substring[0,1] of string2 is equal to string1. Then, return true. 3- Else, check if it is rotated anti-clockwise that means elements are shifted to left. So, check if concatenation of substring[len-2, len-1] with substring[0....len-3] makes it equals to string1. Then return true. 4- Else, return false.
Below is the implementation of above approach.
Exercise : Check if string2 is obtained by rotating string1 by k places.
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if a string can be obtained by rotating another string d places
- String obtained by reversing and complementing a Binary string K times
- Lexicographically smallest string obtained after concatenating array
- Find i'th Index character in a binary string obtained after n iterations
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string can be converted to another string by replacing vowels and consonants
- Queries to check if string B exists as substring in string A
- Check if any permutation of string is a K times repeated string
- Check if a string can become empty by recursively deleting a given sub-string
- Check if left and right shift of any string results into given string
- Check if a string is a scrambled form of another string
- Check if a string contains a palindromic sub-string of even length
- Check whether second string can be formed from characters of first string
- Check if the given string is shuffled substring of another string
- Check if a string can be repeated to make another string
- Check if a given string is a Reverse Bitonic String or not
- Check if it is possible to sort the array after rotating it