Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Print All The Permutations Of a String

Objec­tive: Given a String, print all the per­mu­ta­tions of it.

Input: A String

Out­put: Print all the per­mu­ta­tions of a string

Exam­ple:

Input : abc
Output: abc acb bac bca cba cab

Approach:
  • Take one char­ac­ter at a time and fix it at the first posi­tion. (use swap to put every char­ac­ter at the first posi­tion)
  • make recur­sive call to rest of the characters.
  • use swap to revert the string back to its orig­i­nal form fo next iteration.
Permutations

Per­mu­ta­tions

Com­plete Code:


Out­put :

abc acb bac bca cba cab

You may also like...

  • Pingback: String Permutation | Revo()

  • san­jib

    use sim­ple string based pro­gram as below

    pub­lic class StringPermutations {

    pub­lic sta­tic void main(String[] args) {

    String s = “abcd”;

    String­Per­mu­ta­tions i = new StringPermutations();

    i.permute(“”, s);

    }

    pri­vate void permute(String new­String, String oriString) {

    if (oriString == null || oriString.isEmpty()) {

    System.out.println( newString);

    }

    for (int i = 0; i < oriString.length(); i++) {

    permute(newString.concat(oriString.substring(i, i + 1)), oriString

    .substring(0, i).concat(oriString.substring(i + 1)));

    }

    }

    }

  • rakesh singh