Javascript Program to Check if a string can be obtained by rotating another string d places Last Updated : 19 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An approach to solve the same problem has been discussed here. In this article, reversal algorithm is used to rotate the string to the left and to the right in O(n). If any one of the rotations of str1 is equal to str2 then print Yes else print No. Below is the implementation of the above approach: JavaScript <script> // JavaScript implementation of the approach // Function to reverse an array from left // index to right index (both inclusive) function ReverseArray(arr, left, right) { var temp; while (left < right) { temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } // Function that returns true if str1 can be // made equal to str2 by rotating either // d places to the left or to the right function RotateAndCheck(str1, str2, d) { if (str1.length !== str2.length) return false; // Left Rotation string will contain // the string rotated Anti-Clockwise // Right Rotation string will contain // the string rotated Clockwise var left_rot_str1 = []; var right_rot_str1 = []; var left_flag = true, right_flag = true; var str1_size = str1.length; // Copying the str1 string to left rotation string // and right rotation string for(var i = 0; i < str1_size; i++) { left_rot_str1.push(str1[i]); right_rot_str1.push(str1[i]); } // Rotating the string d positions to the left ReverseArray(left_rot_str1, 0, d - 1); ReverseArray(left_rot_str1, d, str1_size - 1); ReverseArray(left_rot_str1, 0, str1_size - 1); // Rotating the string d positions to the right ReverseArray(right_rot_str1, 0, str1_size - d - 1); ReverseArray(right_rot_str1, str1_size - d, str1_size - 1); ReverseArray(right_rot_str1, 0, str1_size - 1); // Comparing the rotated strings for(var i = 0; i < str1_size; i++) { // If cannot be made equal with left rotation if (left_rot_str1[i] !== str2[i]) { left_flag = false; } // If cannot be made equal with right rotation if (right_rot_str1[i] !== str2[i]) { right_flag = false; } } // If both or any one of the rotations // of str1 were equal to str2 if (left_flag || right_flag) return true; return false; } // Driver code var str1 = "abcdefg"; var str2 = "cdefgab"; // d is the rotating factor var d = 2; // In case length of str1 < d d = d % str1.length; if (RotateAndCheck(str1, str2, d)) document.write("Yes"); else document.write("No"); // This code is contributed by rdtank </script> Output: Yes Time Complexity: O(n) Approach: In this approach we use temp string which is long string with multiplicity of str1 by 2. If we want to rotate the string by d place then we slice the string from n places starting from d. Similarly we rotate in right, For right rotation we use d with value (length of str1) - d. If any one of the rotations of str1 is equal to str2 then print Yes else print No. Below is the implementation of the above approach: JavaScript <script> // Function that returns true if str1 can be // made equal to str2 by rotating either // d places to the left or to the right function RotateAndCheck(str1, str2, d) { if (str1.length !== str2.length) return false; // Making temp to slice the rotated string var temp = str1 + str1; var temp2 = str2 + str2; // Index to slice the string to give rotation var len = str1.length var pos1 = len - d ; // Slicing the string var left_rot_str1 = temp.slice(d, d + len) var right_rot_str2 = temp2.slice(pos1, pos1+len ); // Flags check equality of rotation of string to both the string var flag = left_rot_str1 == str2 ; var flag2 = right_rot_str2 == str1; // Returning if(flag || flag2) return true; return false; } // Driver code var str1 = "abcdefg"; var str2 = "cdefgab"; // d is the rotating factor var d = 2; // In case length of str1 < d d = d % str1.length; if (RotateAndCheck(str1, str2, d)) console.log("Yes"); else console.log("No"); </script> Output: Yes Time Complexity: O(n), where n is the size of the given string. Please refer complete article on Check if a string can be obtained by rotating another string d places for more details! Comment More infoAdvertise with us Next Article Javascript Program to Check if a string can be obtained by rotating another string d places kartik Follow Improve Article Tags : Strings JavaScript Web Technologies DSA rotation Reverse +2 More Practice Tags : ReverseStrings Similar Reads Javascript 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 in two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwiseAsked in: Amazon Int 2 min read Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str 9 min read Check if a string can be obtained by rotating another string 2 places Given two strings, str1 and str2, the task is to determine if str2 can be obtained by rotating str1 exactly 2 places in either a clockwise or anticlockwise direction.Examples: Input: str1 = "amazon", str2 = "azonam" Output: Yes Explanation: Rotating string1 by 2 places in anti-clockwise gives the st 11 min read PHP Program to check a string is a rotation of another string Given the two strings we have to check if one string is a rotation of another string. Examples: Input : $string1 = "WayToCrack", $string2 = "CrackWayTo"; Output : Yes Input : $string1 = "WillPower" $string2 = "lliW"; Output : No. The above problem can be easily solved in other languages by concatena 3 min read Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a 3 min read Javascript Program to 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, 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 2 min read Javascript Program to Check if all array elements can be converted to pronic numbers by rotating digits Given an array arr[] of size N, the task is to check if it is possible to convert all of the array elements to a pronic number by rotating the digits of array elements any number of times. Examples: Input: {321, 402, 246, 299} Output: True Explanation: arr[0] ? Right rotation once modifies arr[0] to 3 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 Javascript Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 5 min read POTD Solutions | 12 Novâ 23 | Check if string is rotated by two places 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 Strings but will also help you build up problem-solving 7 min read Like