Skip to content

Commit fcc812b

Browse files
Merge pull request #165 from UmiKami/153-isRotated
153 is rotated
2 parents 3ac2233 + d2bb3a9 commit fcc812b

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

exercises/153-isRotated/README.es.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Una rotación en una cadena se define como quitar el primer elemento y concatena
1212
let output = isRotated("Hello World", "orldHello W")
1313
console.log(output) // true
1414
```
15+
> Más informacion acerca de la rotación de cadenas: [click aquí](https://www.techiedelight.com/es/find-lexicographically-minimal-string-rotation/)

exercises/153-isRotated/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ A rotation on a string is defined as removing first element and concatenating it
1212
let output = isRotated("Hello World", "orldHello W")
1313
console.log(output) // true
1414
```
15+
16+
> For better understanding of string rotation check this article: [click me](https://javarevisited.blogspot.com/2017/07/2-ways-to-check-if-one-string-is-rotation-of-another-String.html#axzz7tVz1WCRT)
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1+
// NOTE for further explantion of this solution visit: https://www.geeksforgeeks.org/a-program-to-check-if-strings-are-rotations-of-each-other/
12
function isRotated(str1, str2) {
23
// your code here
3-
let fracc = str1.split('');
4-
let aux = [];
5-
for (let i of fracc) aux.push(str2.indexOf(i));
6-
let result = !aux.includes(-1);
7-
return result;
4+
if (str1.length != str2.length) {
5+
return false;
6+
}
7+
8+
let q1 = []
9+
for (let i = 0; i < str1.length; i++)
10+
q1.push(str1[i])
11+
12+
let q2 = []
13+
for (let i = 0; i < str2.length; i++)
14+
q2.push(str2[i])
15+
16+
let k = str2.length
17+
18+
while (k--) {
19+
let ch = q2[0]
20+
q2.shift()
21+
q2.push(ch)
22+
if (JSON.stringify(q2) == JSON.stringify(q1))
23+
return true
24+
}
25+
26+
return false
827
}
928

1029
console.log(isRotated('hello world', 'orldhello w')); // => true
1130
console.log(isRotated('hello world', 'omrel wp')); // => false
31+

exercises/153-isRotated/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ test('The function should return true or false if there is one of the 2 given st
2222
});
2323

2424
test('The function should return true or false if there is one of the 2 given strings rotated.', () => {
25-
expect(isRotated('Learning is fun', 'Lr aen ngiiufs')).toBe(true);
25+
expect(isRotated('Learning is fun', 'Lr aen ngiiufs')).toBe(false);
2626
});
2727

2828
test('The function should return true or false if there is one of the 2 given strings rotated.', () => {
29-
expect(isRotated('Work from Home', 'kfmoemro Hor W')).toBe(true);
29+
expect(isRotated('Work from Home', ' HomeWork from')).toBe(true);
3030
});

0 commit comments

Comments
 (0)