-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.rotation.js
More file actions
48 lines (40 loc) · 1.22 KB
/
string.rotation.js
File metadata and controls
48 lines (40 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @title: String rotation
* @description: check if string is rotation of another
* @author: Thorsten Kober
* @email: [email protected]
*/
function isStringRotation1(str1, str2) {
if (str1.length !== str2.length) return false;
const temp = str1.concat(str1);
if (temp.indexOf(str2) === -1) return false;
return true;
}
function isStringRotation2(str1, str2) {
if (str1.length !== str2.length) return false;
const chars = str1.split('');
for (let i = 0; i < chars.length - 1; i++) {
chars.push(chars.shift());
const current = chars.join('');
if (current === str2) return true;
}
return false;
}
function isStringRotation3(str1, str2) {
for (let i = 0; i < str1.length; i++) {
const part1 = str1.substring(0, i);
const part2 = str1.substring(i);
if (part2 + part1 === str2) return true;
}
return false;
}
// npx jest algorithms/string/string.rotation.js
test('isStringRotation1()', () => {
expect(isStringRotation1('waterbottle', 'rbottlewate')).toBe(true);
});
test('isStringRotation2()', () => {
expect(isStringRotation2('waterbottle', 'rbottlewate')).toBe(true);
});
test('isStringRotation3()', () => {
expect(isStringRotation3('waterbottle', 'rbottlewate')).toBe(true);
});