Generating All Permutations of a String

  1. Recursive Permutation Generation

This approach generates all permutations through recursive swaps. The algorithm fixes each character at the first position and recursively permutes the remaining substring.


public class PermutationGenerator {
    public static void main(String[] args) {
        String text = "abc";
        permute(text.toCharArray(), 0);
    }

    static void permute(char[] elements, int fixedIndex) {
        if (fixedIndex == elements.length - 1) {
            System.out.println(new String(elements));
        }
        for (int i = fixedIndex; i < elements.length; i++) {
            swap(elements, fixedIndex, i);
            permute(elements, fixedIndex + 1);
            swap(elements, fixedIndex, i);
        }
    }

    static void swap(char[] arr, int idx1, int idx2) {
        char tmp = arr[idx1];
        arr[idx1] = arr[idx2];
        arr[idx2] = tmp;
    }
}

Output:


abc
acb
bac
bca
cba
cab
  1. Handling Duplicate Characters

When duplicate characters exist, this implementation uses a Set to store unique permutations while maintaining lexicographical order.


import java.util.LinkedHashSet;
import java.util.Set;

public class UniquePermutations {
    static Set<String> results = new LinkedHashSet<>();
    
    public static void main(String[] args) {
        String text = "aabb";
        permuteUnique(text.toCharArray(), 0);
        results.forEach(System.out::println);
    }

    static void permuteUnique(char[] elements, int fixedIndex) {
        results.add(new String(elements));
        for (int i = fixedIndex; i < elements.length; i++) {
            swap(elements, fixedIndex, i);
            permuteUnique(elements, fixedIndex + 1);
            swap(elements, fixedIndex, i);
        }
    }

    static void swap(char[] arr, int idx1, int idx2) {
        char tmp = arr[idx1];
        arr[idx1] = arr[idx2];
        arr[idx2] = tmp;
    }
}

Output:


aabb
abab
abba
baab
baba
bbaa
  1. Fixed-Length Permutations

This method generates permutations of a specified length by limiting recursion depth and preserving prefix elements.


public class FixedLengthPermutations {
    public static void main(String[] args) {
        char[] elements = {'x','y','z'};
        int length = 2;
        generatePermutations(elements, length, 0);
    }

    static void generatePermutations(char[] elements, int length, int depth) {
        if (depth == length) {
            for (int i = 0; i < length; i++) {
                System.out.print(elements[i]);
            }
            System.out.println();
            return;
        }
        for (int i = depth; i < elements.length; i++) {
            swap(elements, depth, i);
            generatePermutations(elements, length, depth + 1);
            swap(elements, depth, i);
        }
    }

    static void swap(char[] arr, int idx1, int idx2) {
        char tmp = arr[idx1];
        arr[idx1] = arr[idx2];
        arr[idx2] = tmp;
    }
}

Output:


xy
xz
yx
yz
zy
zx

Tags: String Permutations Recursion backtracking Java Algorithms combinatorics

Posted on Wed, 29 Jul 2026 16:44:08 +0000 by abie10