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

121
Visualizações
Get combinations of whole words in a string with rest of the words in javascript

I have following string sentence as input

'test string of whole words'

And as output, I need an array of strings of different combination of words including rest of the words like this

test string of whole words
teststring of whole words
test stringof whole words
test string ofwhole words
test string of wholewords
teststringof whole words
test stringofwhole words
test string ofwholewords
teststringofwhole words
test stringofwholewords
teststringofwholewords

I have tried to write following code to achieve above output but it only works to remove one space at a time. This code is just a trial from me. No need to update/extend it if it is the wrong way. You can start from scratch

function getSpaceIndices(str) {
  let spaceIndices = []
  for (let i = 0; i < str.length; i++) {
    if (str[i] === " ") {
      spaceIndices.push(i)
    }
  }
  return spaceIndices
}

let str = 'test string of whole words'
let strArray = str.split(" ");
let combinationArray = []

// let spaceRemoveCount = 1
let spaceIndices = getSpaceIndices(str)
let currentIndex = 0
while (currentIndex < spaceIndices.length) {
  let tempStr = str.slice(0, spaceIndices[currentIndex]) + str.slice(spaceIndices[currentIndex] + 1);
  combinationArray.push(tempStr);
  currentIndex++
}
console.log(combinationArray);

Please help to achieve above output array from above input string

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Should be able to do it fairly easiy with recursion, just for a start you can try as below

function getSpaceIndices(str) {
  let spaceIndices = []
  for (let i = 0; i < str.length; i++) {
    if (str[i] === " ") {
      spaceIndices.push(i)
    }
  }
  return spaceIndices
}

let str = 'test string of whole words'
let strArray = str.split(" ").filter(s => s != " ");
let combinationArray = []

// let spaceRemoveCount = 1
/*
let spaceIndices = getSpaceIndices(str)
let currentIndex = 0
while (currentIndex < spaceIndices.length) {
  let tempStr = str.slice(0, spaceIndices[currentIndex]) + str.slice(spaceIndices[currentIndex] + 1);
  combinationArray.push(tempStr);
  currentIndex++
}
console.log(combinationArray);
*/

function print(str,current, start, merge){

  let s = ""
  if(merge){
   s =current+str[start];
  }
  else{
  s = current+" "+str[start]
  }
  start++;
  if(start >= str.length){
    console.log(s);
    return;
  }

  print(str,s,start,true);
  print(str,s,start,false);
  
  
}

print(strArray,"",0);

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