0 votes
367 views
in Data Structure And Algorithm by

Generating all permutations of a given string in java. 

For Ex:- String str = "abc";

Output:- [abc, acb, bac, bca, cba, cab]

3 Answers

0 votes
by (2.8k points)

See the following code to generate all string permutations:- 

//Taking string which have to generate the permutations.

private static void allPermutations(String str) {

generateAllPermutations(str.toCharArray(), 0);

}

//Generates all permutations of string

private static void generateAllPermutations(char[] ch, int currentIndex) {

if(currentIndex == ch.length-1) {

System.out.println(String.copyValueOf(ch));

}

for(int i=currentIndex; i<ch.length; i++) {

swap(ch, currentIndex, i);

generateAllPermutations(ch, currentIndex+1);

swap(ch, currentIndex, i);

}

}

//Swap character

private static void swap(char[] ch, int currentIndex, int i) {

char tmp = ch[currentIndex];

ch[currentIndex] = ch[i];

ch[i] = tmp;

}

Output:-

Permutations:-

abc

acb

bac

bca

cba

cab

0 votes
by (2.8k points)
If you want to generate the string permutations then simply copy the following code and run it.

        //n*!n

//Input is an empty string the only permutation is the empty string.

//Try each of the letters in turn as the first letter and then find all the permutations of the remaining letters using a recursive call.

private static void permutation(String perm, String word) {

if (word.isEmpty()) {

System.out.println(perm + word);

} else {

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

permutation(perm + word.charAt(i), word.substring(0, i) + word.substring(i + 1, word.length()));

}

}

}

Output:-

Permutations:-

abc

acb

bac

bca

cab

cba
0 votes
by (2.8k points)
To generate all permutations of string by iterative way. The following code generate the all possible strings:-

         /** Returns an array list containing all

permutations of the characters in s.

      */

public static ArrayList<String> permutations(String s) {

    ArrayList<String> perms = new ArrayList<>();

    int slen = s.length();

    if (slen > 0) {

        // Add the first character from s to the perms array list.

        perms.add(Character.toString(s.charAt(0)));

        // Repeat for all additional characters in s.

        for (int i = 1;  i < slen;  ++i) {

            // Get the next character from s.

            char c = s.charAt(i);

            // For each of the strings currently in perms do the following:

            int size = perms.size();

            for (int j = 0;  j < size;  ++j) {

                // 1. remove the string

                String p = perms.remove(0);

                int plen = p.length();

                // 2. Add plen + 1 new strings to perms.  Each new string

                //    consists of the removed string with the character c

                //    inserted into it at a unique location.

                for (int k = 0;  k <= plen;  ++k) {

                    perms.add(p.substring(0, k) + c + p.substring(k));

                }

            }

        }

    }

    return perms;

}

Output:-

All permutation using iterative method

cba

bca

bac

cab

acb

abc

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...