Skip to main content
deleted 3 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

permutations Permutations of a string

I wrote the following code to permute the characters of a string assuming there is no repeated character.

Any comments please?

Also, I also want to make sure:

The the space complexity of this algorithm is O(n*n!)\$O(n*n!)\$: There are n!\$n!\$ recursive calls, for each of these calls, I copy a string of size n am\$n\$. Am I right?

Time complexity: O(n!)\$O(n!)\$: n!\$n!\$ recursive calls

Is it possible to domake it more efficient?

  public static void permute(String str, String prefix){
    if (str.length() == 0) System.out.println(prefix);

    for (int i = 0; i < str.length(); i++){
      String c = Character.toString(str.charAt(i));
      String rest = str.substring(0, i) + str.substring(i+1);
      permute(rest, prefix + c);
    }
    
  }

permutations of a string

I wrote the following code to permute the characters of a string assuming there is no repeated character.

Any comments please?

Also, I want to make sure:

The space complexity of this algorithm is O(n*n!): There are n! recursive calls, for each of these calls, I copy a string of size n am I right?

Time complexity: O(n!): n! recursive calls

Is it possible to do more efficient?

  public static void permute(String str, String prefix){
    if (str.length() == 0) System.out.println(prefix);

    for (int i = 0; i < str.length(); i++){
      String c = Character.toString(str.charAt(i));
      String rest = str.substring(0, i) + str.substring(i+1);
      permute(rest, prefix + c);
    }
    
  }

Permutations of a string

I wrote the following code to permute the characters of a string assuming there is no repeated character.

I also want to make sure the space complexity of this algorithm is \$O(n*n!)\$: There are \$n!\$ recursive calls, for each of these calls, I copy a string of size \$n\$. Am I right?

Time complexity: \$O(n!)\$: \$n!\$ recursive calls

Is it possible to make it more efficient?

  public static void permute(String str, String prefix){
    if (str.length() == 0) System.out.println(prefix);

    for (int i = 0; i < str.length(); i++){
      String c = Character.toString(str.charAt(i));
      String rest = str.substring(0, i) + str.substring(i+1);
      permute(rest, prefix + c);
    }
    
  }
Source Link
giulio
  • 743
  • 6
  • 13

permutations of a string

I wrote the following code to permute the characters of a string assuming there is no repeated character.

Any comments please?

Also, I want to make sure:

The space complexity of this algorithm is O(n*n!): There are n! recursive calls, for each of these calls, I copy a string of size n am I right?

Time complexity: O(n!): n! recursive calls

Is it possible to do more efficient?

  public static void permute(String str, String prefix){
    if (str.length() == 0) System.out.println(prefix);

    for (int i = 0; i < str.length(); i++){
      String c = Character.toString(str.charAt(i));
      String rest = str.substring(0, i) + str.substring(i+1);
      permute(rest, prefix + c);
    }
    
  }