Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda