diff --git a/exercises/153-isRotated/README.es.md b/exercises/153-isRotated/README.es.md index 48329ce02..62c93b973 100644 --- a/exercises/153-isRotated/README.es.md +++ b/exercises/153-isRotated/README.es.md @@ -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/) \ No newline at end of file diff --git a/exercises/153-isRotated/README.md b/exercises/153-isRotated/README.md index 9de64815c..fd9605714 100644 --- a/exercises/153-isRotated/README.md +++ b/exercises/153-isRotated/README.md @@ -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) diff --git a/exercises/153-isRotated/solution.hide.js b/exercises/153-isRotated/solution.hide.js index 908d49865..5e416d566 100644 --- a/exercises/153-isRotated/solution.hide.js +++ b/exercises/153-isRotated/solution.hide.js @@ -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 + diff --git a/exercises/153-isRotated/test.js b/exercises/153-isRotated/test.js index 9afcfac02..51ff6b160 100644 --- a/exercises/153-isRotated/test.js +++ b/exercises/153-isRotated/test.js @@ -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); });