Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!