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

192
Visualizações
String permutaion

I have found the following Java code. It counts all the permutations of a string. However, I can't understand what it does inside the for loop of the permutation method. More specifically I can't understand the purpose of the rem string and the recursive call.

Is there any thoughts on this?

Thanks.

void permutaion(String str){
    permutaion(str,"");
}

void permutaion(String str, String prefix){
    if(str.length() == 0){
        System.out.println(prefix);
    } else{
        for(int i=0; i < str.length(); i++){
            String rem = str.substring(0,i) + str.substring(i+1);
            permutaion(rem, prefix + str.charAt(i));
        }
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You should run this code by hand on paper to see it's magic and understand it :) The recursive on permutation function is for permutation input string, and the for loop is for all possible case. The prefix is the permutation of the input string when a recursive finish.

over 4 years ago · Santiago Trujillo Relatório

0

The basic idea is this: to get the permutations of a given N-character string you:

  1. Take the first letter of the original string and append to it all the permutations of the remaining N - 1 characters;
  2. Take the first two letters of the original string and append to them all the permutations of the remaining N - 2 characters;
  3. Take the first three letters of the original string and append to them all the permutations of the remaining N - 3 characters ...
over 4 years ago · Santiago Trujillo 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