Search⌘ K
AI Features

String Rotation

Explore methods to verify if one string is a rotation of another by implementing two solutions. Understand the time and space complexity of a brute-force approach and an optimized method using string concatenation, enhancing your ability to handle string rotation problems efficiently.

We'll cover the following...

String Rotation

Instructions

Create a function that takes in 2 strings as parameters.

Return true if the strings are rotations of each other. Otherwise, return false.

Input: String, String

Output: Boolean

Examples

The following sets of ...

Node.js
function stringRotation(str1, str2) {
// Your code here
}

Solution 1

Node.js
function stringRotation(str1, str2) {
if(str1.length !== str2.length) {
return false;
}
for(let i = 0; i < str1.length; i++) {
const rotation = str1.slice(i, str1.length) + str1.slice(0, i);
if(rotation === str2) {
return true;
}
}
return false;
}
...