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
Is the second string a rotated version of the first string JavaScript
We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string.
For example ? If the input strings are ?
const str1 = 'abcde'; const str2 = 'cdeab';
Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1.
Understanding String Rotation
A string rotation means taking some characters from the beginning of a string and moving them to the end. For example, rotating "abcde" by 2 positions gives "cdeab".
Method 1: Using Concatenation Approach
The most elegant solution is to check if str2 exists as a substring in str1 + str1. This works because concatenating a string with itself contains all possible rotations.
const isRotatedConcat = (str1, str2) => {
// Check if lengths are equal and strings are not empty
if (str1.length !== str2.length || str1.length === 0) {
return str1.length === str2.length;
}
// Check if str2 is a substring of str1 + str1
return (str1 + str1).includes(str2);
};
const str1 = 'abcde';
const str2 = 'cdeab';
console.log(isRotatedConcat(str1, str2));
// Test with more examples
console.log(isRotatedConcat('hello', 'llohe')); // true
console.log(isRotatedConcat('abcd', 'bcda')); // true
console.log(isRotatedConcat('abcd', 'bcde')); // false
true true true false
Method 2: Using Slice and Compare
This approach generates all possible rotations of str1 and checks if any matches str2.
const isRotatedSlice = (str1, str2) => {
if (str1.length !== str2.length) {
return false;
}
if (str1.length === 0) {
return true;
}
for (let i = 0; i < str1.length; i++) {
const rotated = str1.slice(i) + str1.slice(0, i);
if (rotated === str2) {
return true;
}
}
return false;
};
const str1 = 'abcde';
const str2 = 'cdeab';
console.log(isRotatedSlice(str1, str2));
// Show the rotation process
console.log("Rotations of 'abcde':");
for (let i = 0; i < str1.length; i++) {
const rotated = str1.slice(i) + str1.slice(0, i);
console.log(`Position ${i}: ${rotated}`);
}
true Rotations of 'abcde': Position 0: abcde Position 1: bcdea Position 2: cdeab Position 3: deabc Position 4: eabcd
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Concatenation | O(n) | O(n) | High |
| Slice and Compare | O(n²) | O(n) | Medium |
Edge Cases
// Test edge cases
console.log("Edge cases:");
console.log(isRotatedConcat('', '')); // true - both empty
console.log(isRotatedConcat('a', 'a')); // true - same single char
console.log(isRotatedConcat('abc', 'ab')); // false - different lengths
console.log(isRotatedConcat('aaa', 'aaa')); // true - repeated chars
Edge cases: true true false true
Conclusion
The concatenation method is more efficient and cleaner for checking string rotations. It leverages the fact that all rotations of a string appear as substrings when the string is concatenated with itself.
