String Rotation

We'll discuss how to determine if two strings are rotations of one another. We'll go over a brute-force solution and an elegant solution.

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 strings are rotations:

"rotation"  "tationro" "tionrota"

"javascript"  "scriptjava"  "iptjavascr"

"RotateMe"  "teMeRota"  "eRotateM"

Hints

  • You may have to spend time thinking about how
...
Press + to interact
function stringRotation(str1, str2) {
// Your code here
}

Solution 1

Press + to interact
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;
}
...