Print All The Permutations Of a String

Objective: Given a String, print all the permutations of it.

Input: A String

Output: Print all the permutations of a string

Example:

Input : abc
Output: abc acb bac bca cba cab

Approach:
  • Take one character at a time and fix it at the first position. (use swap to put every character at the first position)
  • make recursive call to rest of the characters.
  • use swap to revert the string back to its original form fo next iteration.

Permutations

Complete Code:


Output :

abc acb bac bca cba cab

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

  • Pingback: String Permutation | Revo()

  • sanjib

    use simple string based program as below

    public class StringPermutations {

    public static void main(String[] args) {

    String s = “abcd”;

    StringPermutations i = new StringPermutations();

    i.permute(“”, s);

    }

    private void permute(String newString, 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
%d bloggers like this: