Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Check if a string can be obtained by rotating another string d places
Rotating a string means moving characters to the left or right by a specified number of positions. We'll check if one string can be obtained by rotating another string exactly d places in either direction.
Problem Understanding
Given two strings and a rotation count d, we need to determine if the first string can be transformed into the second string by rotating it d positions left or right.
Example
String 1: "abcdef", String 2: "defabc", Rotations: 3
Left rotation by 3: "abcdef" ? "bcdefa" ? "cdefab" ? "defabc" ?
Approach 1: Left Rotation
In left rotation, we move characters from the beginning to the end. We use the substring method to split and rearrange the string.
// Function to perform left rotation
function leftRotation(str, k) {
var len = str.length;
k = k % len; // Handle rotations greater than string length
return str.substring(k) + str.substring(0, k);
}
// Function to check if string can be converted by left rotation
function checkLeftRotation(string1, string2, rotations) {
var len1 = string1.length;
var len2 = string2.length;
// Strings must have equal length
if (len1 !== len2) {
return false;
}
// Rotate first string and compare
var rotatedString = leftRotation(string1, rotations);
return rotatedString === string2;
}
// Test the function
var string1 = "abcdef";
var string2 = "defabc";
var rotations = 3;
if (checkLeftRotation(string1, string2, rotations)) {
console.log("Yes, '" + string1 + "' can be converted to '" + string2 + "' by " + rotations + " left rotations");
} else {
console.log("No, conversion not possible with " + rotations + " left rotations");
}
Yes, 'abcdef' can be converted to 'defabc' by 3 left rotations
Approach 2: Right Rotation
In right rotation, we move characters from the end to the beginning. The logic is reversed compared to left rotation.
// Function to perform right rotation
function rightRotation(str, k) {
var len = str.length;
k = k % len; // Handle rotations greater than string length
return str.substring(len - k) + str.substring(0, len - k);
}
// Function to check if string can be converted by right rotation
function checkRightRotation(string1, string2, rotations) {
var len1 = string1.length;
var len2 = string2.length;
// Strings must have equal length
if (len1 !== len2) {
return false;
}
// Rotate first string and compare
var rotatedString = rightRotation(string1, rotations);
return rotatedString === string2;
}
// Test the function
var string1 = "abcdef";
var string2 = "bcdefa";
var rotations = 1;
if (checkRightRotation(string1, string2, rotations)) {
console.log("Yes, '" + string1 + "' can be converted to '" + string2 + "' by " + rotations + " right rotation(s)");
} else {
console.log("No, conversion not possible with " + rotations + " right rotation(s)");
}
Yes, 'abcdef' can be converted to 'bcdefa' by 1 right rotation(s)
Combined Approach: Check Both Directions
Often we need to check if rotation is possible in either direction without specifying which one.
function canRotateString(str1, str2, d) {
// Check if lengths are equal
if (str1.length !== str2.length) {
return { possible: false, direction: "none" };
}
// Check left rotation
var leftRotated = leftRotation(str1, d);
if (leftRotated === str2) {
return { possible: true, direction: "left" };
}
// Check right rotation
var rightRotated = rightRotation(str1, d);
if (rightRotated === str2) {
return { possible: true, direction: "right" };
}
return { possible: false, direction: "none" };
}
// Test cases
var testCases = [
{ str1: "abcdef", str2: "defabc", rotations: 3 },
{ str1: "hello", str2: "llohe", rotations: 2 },
{ str1: "abc", str2: "def", rotations: 1 }
];
testCases.forEach(function(test, index) {
var result = canRotateString(test.str1, test.str2, test.rotations);
console.log("Test " + (index + 1) + ": " +
(result.possible ?
"Possible via " + result.direction + " rotation" :
"Not possible"));
});
Test 1: Possible via left rotation Test 2: Possible via left rotation Test 3: Not possible
Time and Space Complexity
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Left Rotation | O(n) | O(n) - for substring creation |
| Right Rotation | O(n) | O(n) - for substring creation |
| Combined Check | O(n) | O(n) - for substring creation |
Key Points
- Use modulo operation (k % len) to handle rotations greater than string length
- Always check if strings have equal length before rotation
- Left rotation: move first k characters to end
- Right rotation: move last k characters to beginning
Conclusion
String rotation problems can be solved efficiently using substring operations. The key is understanding the direction of rotation and using modulo arithmetic to handle edge cases. Both approaches have linear time complexity and are practical for most use cases.
