Given two strings s1 and s2, check whether s2 is a rotation of s1.
Input : ABACD, CDABA Output : True Input : GEEKS, EKSGE Output : True
We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm’s lps (longest proper prefix which is also suffix) construction, which will help in finding the longest match of the prefix of string b and suffix of string a. By which we will know the rotating point, from this point match the characters. If all the characters are matched, then it is a rotation, else not.
Below is the basic implementation of the above approach.
Time Complexity : O(n)
Auxiliary Space : O(n)
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.
- A Program to check if strings are rotations of each other or not
- Number of strings which starts and ends with same character after rotations
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Check if two numbers are bit rotations of each other or not
- Check if all rows of a matrix are circular rotations of each other
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check whether Strings are k distance apart or not
- Check if two strings are permutation of each other
- Check if two strings are k-anagrams or not
- Check whether two strings are anagram of each other
- Program to check if two strings are same or not
- Check if two given strings are isomorphic to each other
- Check whether two strings are anagrams of each other using unordered_map in C++
- Check if two strings have a common substring
- Check whether two strings are equivalent or not according to given condition
- Check whether two strings contain same characters in same order
- Check if two strings are same ignoring their cases
- Check duplicates in a stream of strings
- Check if all the palindromic sub-strings are of odd length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m