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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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 | Set 2
- Find i'th Index character in a binary string obtained after n iterations
- 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 converted to another string by replacing vowels and consonants
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if any permutation of string is a K times repeated string
- Check if left and right shift of any string results into given string
- Queries to check if string B exists as substring in string A
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string is a scrambled form of another string
- Check if a string contains a palindromic sub-string of even length