Javascript Program to Check if strings are rotations of each other or not | Set 2 Last Updated : 23 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. JavaScript // javascript program to check if two strings are rotations // of each other. function isRotation(a, b) { let n = a.length; let m = b.length; if (n != m) return false; // create lps that will hold the longest // prefix suffix values for pattern let lps = Array.from({ length: n }, (_, i) => 0); // length of the previous longest prefix suffix let len = 0; let i = 1; lps[0] = 0; // lps[0] is always 0 // the loop calculates lps[i] for i = 1 to n-1 while (i < n) { if (a.charAt(i) == b.charAt(len)) { lps[i] = ++len; ++i; } else { if (len == 0) { lps[i] = 0; ++i; } else { len = lps[len - 1]; } } } i = 0; // match from that rotating point for (k = lps[n - 1]; k < m; ++k) { if (b.charAt(k) != a.charAt(i++)) return false; } return true; } // Driver code let s1 = "ABACD"; let s2 = "CDABA"; console.log(isRotation(s1, s2) ? "1" : "0"); Output1 Complexity Analysis:Time 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 Javascript Program to Check if strings are rotations of each other or not | Set 2 K kartik Follow Improve Article Tags : Strings Pattern Searching JavaScript Web Technologies DSA rotation +2 More Practice Tags : Pattern SearchingStrings Similar Reads Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: 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 (long 6 min read Javascript Program to Check if two numbers are bit rotations of each other or not Given two positive integers x and y (0 < x, y < 2^32), check if one integer is obtained by rotating bits of the other. Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end.Examples: Input : a 3 min read Check if two strings are permutation of each other in JavaScript In this approach, we are going to discuss how we can check if two strings are permutations of each other or not using JavaScript language. If two strings have the same number of characters rather than having the same position or not it will be a permutation. Example: Input: "pqrs" , "rpqs"Output: Tr 4 min read Javascript 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: YesAll rows are rotated permutationof each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: NoExplanation : As 3, 2, 1 is not 2 min read POTD Solutions | 14 Nov'23 | Check if strings are rotations of each other or not View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of String but will also help you build up problem-solving 3 min read Like