Skip to content

Commit

Permalink
Merge pull request mouredev#4978 from RodriJOk/main
Browse files Browse the repository at this point in the history
Reto mouredev#36 - Javascript
  • Loading branch information
Roswell468 authored Sep 12, 2023
2 parents 149592b + 2bb8d16 commit 0e00181
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Retos/Reto #36 - PERMUTACIONES [Media]/javascript/RodriJOk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ## Enunciado
/*
* Crea un programa que sea capaz de generar e imprimir todas las
* permutaciones disponibles formadas por las letras de una palabra.
* - Las palabras generadas no tienen por qué existir.
* - Deben usarse todas las letras en cada permutación.
* - Ejemplo: sol, slo, ols, osl, los, lso
*/

function generatePermutations(word) {
const result = [];

function permute(current, remaining) {
if (remaining.length === 0) {
result.push(current);
return;
}

for (let i = 0; i < remaining.length; i++) {
const nextChar = remaining[i];
const rest = remaining.slice(0, i) + remaining.slice(i + 1);
permute(current + nextChar, rest);
}
}

permute('', word);
return [...new Set(result)];
}

console.log(generatePermutations("sol"))
console.log(generatePermutations("hola"))
console.log(generatePermutations("devs"))
console.log(generatePermutations("rodri"))

0 comments on commit 0e00181

Please sign in to comment.