-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCheckIfStringIsRotatedByTwoPlaces.js
More file actions
59 lines (54 loc) · 1.26 KB
/
Copy pathCheckIfStringIsRotatedByTwoPlaces.js
File metadata and controls
59 lines (54 loc) · 1.26 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
49
50
51
52
53
54
55
56
57
58
59
/**
* @param {string} str1
* @param {string} str2
* @returns {boolean}
*/
class Solution
{
//Function to check if a string can be obtained by rotating
//another string by exactly 2 places.
// TC: O(N) SC: O(1)
isRotated(a, b)
{
// code here
if(a.length !== b.length) return false;
let index = 2;
let flag = true;
for(let i=0; i<a.length; i++) {
index = index % b.length;
if(a[i] !== b[index]) {
flag = false;
break;
}
else {
index++;
}
}
if(flag === true) return true;
else {
index = b.length - 2;
for(let i=0; i<a.length; i++) {
index = index % b.length;
if(a[i] !== b[index]) {
return false;
}
else {
index++;
}
}
return true;
}
return true;
}
}
// TC: O(N) SC: O(N)
isRotated(a, b)
{
// code here
let str = a.slice(2) + a.slice(0,2);
if(str === b) return true;
else {
str = a.slice(-2) + a.slice(0, -2);
return str === b;
}
}