Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exercises/153-isRotated/README.es.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Una rotación en una cadena se define como quitar el primer elemento y concatena
let output = isRotated("Hello World", "orldHello W")
console.log(output) // true
```
> Más informacion acerca de la rotación de cadenas: [click aquí](https://www.techiedelight.com/es/find-lexicographically-minimal-string-rotation/)
2 changes: 2 additions & 0 deletions exercises/153-isRotated/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ A rotation on a string is defined as removing first element and concatenating it
let output = isRotated("Hello World", "orldHello W")
console.log(output) // true
```

> 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)
30 changes: 25 additions & 5 deletions exercises/153-isRotated/solution.hide.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
// NOTE for further explantion of this solution visit: https://www.geeksforgeeks.org/a-program-to-check-if-strings-are-rotations-of-each-other/
function isRotated(str1, str2) {
// your code here
let fracc = str1.split('');
let aux = [];
for (let i of fracc) aux.push(str2.indexOf(i));
let result = !aux.includes(-1);
return result;
if (str1.length != str2.length) {
return false;
}

let q1 = []
for (let i = 0; i < str1.length; i++)
q1.push(str1[i])

let q2 = []
for (let i = 0; i < str2.length; i++)
q2.push(str2[i])

let k = str2.length

while (k--) {
let ch = q2[0]
q2.shift()
q2.push(ch)
if (JSON.stringify(q2) == JSON.stringify(q1))
return true
}

return false
}

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

4 changes: 2 additions & 2 deletions exercises/153-isRotated/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ test('The function should return true or false if there is one of the 2 given st
});

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

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