Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
Permutation using Recursion in Javascript

Through youtube, I've learned to obtain a multi-dimensional output of permutations using recursion, i.e anagrams with passing in an array of letters as an argument (i.e ['a', 'b', 'c']) but is there a way to modify the code to pass in a string instead 'abc' to obtain the output below??

Into [ 'abc', 'bac', bca', 'acb', 'cab', 'cba' ]

This is my code. Is there a way to modify to obtain the desired result, using a string as the argument(i.e 'abc')? Thank you, much appreciated

function anagrams(inputString){
  if(inputString.length ===0) return [[]];
  const stringArr = inputString;
  const firstEl = stringArr[0];
  const restArr = stringArr.slice(1);
  const anagramWithoutFirst = anagrams(restArr);
  const masterArr = [];

  anagramWithoutFirst.forEach(anagram => {
    for (let i=0; i<=anagram.length; i++){
      const anagramWithFirst = [...anagram.slice(0, i), firstEl, ...anagram.slice(i)]
      masterArr.push(anagramWithFirst)
    }
  })
  return masterArr
}

console.log(anagrams(['a', 'b', 'c']))

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This can be achieved using a higher order function which takes in the original function and returns a new function which splits the text up, calls the original function then joins the result back together

function anagrams(inputString){
  if(inputString.length ===0) return [[]];
  const stringArr = inputString;
  const firstEl = stringArr[0];
  const restArr = stringArr.slice(1);
  const anagramWithoutFirst = anagrams(restArr);
  const masterArr = [];

  anagramWithoutFirst.forEach(anagram => {
    for (let i=0; i<=anagram.length; i++){
      const anagramWithFirst = [...anagram.slice(0, i), firstEl, ...anagram.slice(i)]
      masterArr.push(anagramWithFirst)
    }
  })
  return masterArr
}

function splitAndUnsplit(f) {
  return (str) => (f(str.split('')).map(xs => xs.join('')))
}

console.log(splitAndUnsplit(anagrams)('abc'))
console.log(anagrams(['a', 'b', 'c']))

Edit:

To expand on @Scott Sauyet's great comment, we need two sets of parens as we are calling two functions.


function splitAndUnsplit(f) {
  return function(str){
    const inputArr  = str.split('') 
    const outputArr = f(inputArr)
    const outputStr = outputArr.map(xs => xs.join('')))
    return outputStr
  }
}

The first set of parens is used to create a function which will accept and return a string, and the second set is required to call that function with a supplied value

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda