Python3 Program to Check if strings are rotations of each other or not | Set 2 Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABAOutput : TrueInput : GEEKS, EKSGEOutput : TrueWe 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. Python # Python program to check if # two strings are rotations # of each other def isRotation(a: str, b: str) -> bool: n = len(a) m = len(b) if (n != m): return False # create lps[] that # will hold the longest # prefix suffix values # for pattern lps = [0 for _ in range(n)] # length of the previous # longest prefix suffix length = 0 i = 1 # lps[0] is always 0 lps[0] = 0 # the loop calculates # lps[i] for i = 1 to n-1 while (i < n): if (a[i] == b[length]): length += 1 lps[i] = length i += 1 else: if (length == 0): lps[i] = 0 i += 1 else: length = lps[length - 1] i = 0 # Match from that rotating # point for k in range(lps[n - 1], m): if (b[k] != a[i]): return False i += 1 return True # Driver code if __name__ == "__main__": s1 = "ABACD" s2 = "CDABA" print("1" if isRotation(s1, s2) else "0") # This code is contributed by sanjeev2552 Output: 1Time Complexity: O(n) Auxiliary Space: O(n) Please refer complete article on Check if strings are rotations of each other or not | Set 2 for more details! Comment More infoAdvertise with us Next Article Python3 Program to Check if strings are rotations of each other or not | Set 2 K kartik Follow Improve Article Tags : Strings Pattern Searching Python Python Programs DSA rotation +2 More Practice Tags : Pattern SearchingpythonStrings Similar Reads Python3 Program to Check if two numbers are bit rotations of each other or not Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info 3 min read Python3 Program to Check if all rows of a matrix are circular rotations of each other Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1Output: Yes,All rows are rotated permutation of each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: No,As 3, 2, 1 is not a rotated 2 min read Python Program To Check Whether Two Strings Are Anagram Of Each Other Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an anagram of each other. We strongly recommend that you 8 min read Python3 Program to Check if a string can be obtained by rotating another string 2 places Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwiseAsked in: Amazon Interv 2 min read Python Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 3 min read Like