View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Permutation Java: From Basic Concepts to Advanced Techniques and Debugging with Coding Examples

By Rohan Vats

Updated on Jun 02, 2025 | 21 min read | 7.6K+ views

Share:

Did you know? Over 90% of Fortune 500 companies use Java for their software development needs in 2025, highlighting its dominance in the enterprise sector.

Permutation in Java is the process of generating all possible ordered arrangements of a set of elements, where the order significantly affects the outcome. Understanding Java permute techniques is essential for tackling challenges in cryptography, algorithm design, and combinatorial problems that require systematic exploration of sequences.

This blog explains the fundamentals of permutation Java, practical approaches to creating permutations, and common debugging methods. You will find clear coding examples illustrating how to write and optimize permute Java functions for specific computational tasks.

Ready to create efficient and reliable programs with Java? Begin your learning path with online software engineering courses to gain the skills needed for writing clean code and building scalable Java applications.

Understanding Permutation Java: An Overview of Basics

A permutation refers to an arrangement of objects in a specific order. For a particular set, the number of possible permutations is calculated based on how many objects are selected and their order of arrangement. 

When considering permutations, the order in which the objects appear is important. It is then used to calculate the number of ways to arrange or order items from a set.

To strengthen your skills in Java and other advanced technologies, check these leading programs:

Understanding Permutation in Mathematics

Permutation is a way to find out how many different ways you can arrange a certain number of items when the order matters. It answers questions like: "How many ways can I arrange 3 letters?" or "How many ways can I line up 5 people?"

The formula to calculate permutations is:

 

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Where, 

  • n! (n factorial) means multiplying all whole numbers from n down to 1. For example, 3! = 3 × 2 × 1 = 6.
  • n is the total number of items you have.
  • r is the number of items you want to arrange or select.

Breaking down the formula:

  • The numerator, n!n!, counts all the possible ways to arrange all n items.
  • The denominator, (n−r)!(n - r)!, removes the arrangements of the leftover items you aren’t arranging.

Example: How many ways can you arrange the letters in “ABD”?

  • Total letters, n = 3 (A, B, and D)
  • Number of letters to arrange, r = 3 (since you want to arrange all letters)

Using the formula:

We know:

  • 3! = 3 × 2 × 1 = 6
  • 0! is defined as 1

So,

This means there are 6 different ways to arrange the letters A, B, and D

This concept helps solve many problems where the order of arrangement matters, such as passwords, seating arrangements, or scheduling tasks.

Also Read: Permutation vs Combination: Difference between Permutation and Combination

Now that you’ve explored the mathematical concept of permutations, let’s examine the different types of permutations.

Types of Permutations

Permutation refers to different ways of arranging a set of objects. There are three main types of permutations: ordered permutations, unique permutations, and permutations with repetition. 

Let’s look at these three types in detail.

1. Ordered Permutations
Ordered permutations are those in which the arrangement of the objects is important. When calculating ordered permutations, each distinct arrangement of objects is considered different, even if the objects themselves are the same.

Example: For the set of letters "A", "B", and "C", the ordered permutations would be: ABC, ACB, BAC, BCA, CAB, CBA.

Permute Java Example:

import java.util.ArrayList;
import java.util.List;

public class OrderedPermutations {

    public static void permute(String str, String ans) {
        if (str.length() == 0) {
            System.out.println(ans);
            return;
        }
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            // Remaining string after removing character ch
            String ros = str.substring(0, i) + str.substring(i + 1);
            permute(ros, ans + ch);
        }
    }

    public static void main(String[] args) {
        String input = "ABC";
        permute(input, "");
    }
}

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation:

The method recursively builds permutations by picking each character, removing it from the string, and appending it to the answer string. It continues until the input string is empty, printing the full permutation.

Also Read: Math for Data Science: A Beginner’s Guide to Important Concepts

2. Unique Permutations
Unique permutations are used when there are repeated elements in the set. The focus is to find distinct arrangements by removing any duplicate arrangements due to the repetition of elements.

Example: Consider the set "AAB". The unique permutations would be AAB, ABA, BAA

Permute Java Example:

import java.util.HashSet;

public class UniquePermutations {

    public static void permuteUnique(String str, String ans, HashSet<String> set) {
        if (str.length() == 0) {
            if (!set.contains(ans)) {
                set.add(ans);
                System.out.println(ans);
            }
            return;
        }
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            String ros = str.substring(0, i) + str.substring(i + 1);
            permuteUnique(ros, ans + ch, set);
        }
    }

    public static void main(String[] args) {
        String input = "AAB";
        HashSet<String> set = new HashSet<>();
        permuteUnique(input, "", set);
    }
}

Output:

AAB
ABA
BAA

Explanation:

Here, a HashSet is used to store permutations as they are generated. Before printing, the program checks if the permutation is already printed to avoid duplicates.

3. Permutations with Repetition
Permutations with repetition occur when objects are repeated, but you are allowed to use the same objects multiple times in the arrangement. This is common in cases where you have multiple slots to fill and can use the same item more than once.

Example: For the set "A", "B", and "C", if you are allowed to repeat the elements and select 2 objects at a time, the permutations with repetition are AA, AB, AC, BA, BB, BC, CA, CB, CC.

Permutation Java Example: 

public class PermutationsWithRepetition {

    public static void permuteWithRepetition(char[] set, int length, String ans) {
        if (ans.length() == length) {
            System.out.println(ans);
            return;
        }
        for (char c : set) {
            permuteWithRepetition(set, length, ans + c);
        }
    }

    public static void main(String[] args) {
        char[] set = {'A', 'B', 'C'};
        int length = 2;
        permuteWithRepetition(set, length, "");
    }
}

Output:

AA
AB
AC
BA
BB
BC
CA
CB
CC

Explanation:

In this permutation Java, the function builds permutations by adding any character from the set at each step until the desired length is reached. Since repetition is allowed, it does not remove characters from the set after selecting them.

Want to master Java’s permutation algorithms and avoid common pitfalls? Enroll in upGrad's Java Object-Oriented Programming course today!

Now that you’ve explored the basic concept of permutations and their types, let’s understand how to implement it using Java. 

Getting Started with Permutation Java: Simplified

Before you start implementing Permute Java, you need to set up a Java development environment, which will provide an efficient platform for writing, testing, and executing your code to implement permutations.

Here are the steps to setup a Java environment on your systems. 

1. Install Java Development Kit (JDK)

Visit the official website of Oracle and download the latest version of JDK (Java Development Kit). You can follow the installation instructions based on your operating system (Windows, macOS, or Linux).

2. Set up Java Environment Variables

Once Java is installed, you must set up environment variables to allow Java to be accessed from the command line. There are different ways for different operating systems.

  • Windows

Go to Control Panel > System > Advanced System Settings > Environment Variables, and set the JAVA_HOME variable to your Java installation path (e.g., C:\Program Files\Java\jdk-11).

  • Linux/MacOS

Open your terminal and add the following to your shell configuration file (~/.bashrc or ~/.zshrc):

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH

Run the command, run source ~/.bashrc or source ~/.zshrc

3. Install an Integrated Development Environment (IDE)

You need to download an IDE to write your Java code. Popular Java IDEs include IntelliJ IDEA, NetBeans, and Eclipse. After you download and install your IDE, configure it to use the installed JDK.

Also Read: IntelliJ IDEA vs. Eclipse: The Holy War!

4. Test Your Setup

To confirm that Java is set up properly, open a terminal/command prompt and type:

java -version

The output will display the version of Java.

Also Read: Top 45+ Must-Know Linux Commands Interview Questions and Answers for 2025

Now that you’ve explored ways to install Java in your systems, you need to understand certain key approaches before writing codes for implementing permutation Java. Recursion, backtracking, and iterative methods are the three techniques used.

Here are the three main techniques for permutation Java.

1. Recursion

Recursion is a technique where a function calls itself to solve a smaller instance of the problem. In the case of permutations, you can use recursion to fix one element and recursively find the permutations of the remaining elements.

For large datasets, using recursion may lead to stack overflow errors. Consider using tail recursion or iterative approaches for better performance.

Approach:

  • Base Case: When the string length is 1, return the string as a valid permutation.
  • Recursive Case: For each character in the string, swap it with the first character and recursively generate permutations of the rest.

Example: Permutation Java using Recursion

Here's how recursion works in generating permutations:

public class PermutationRecursion {

    public static void permute(String str, String ans) {
        // Base case: If the string is empty, print the answer
        if (str.length() == 0) {
            System.out.println(ans);
            return;
        }

        // Loop through the string and swap each character
        for (int i = 0; i < str.length(); i++) {
            // Choose the current character and recursively call permute on the remaining substring
            char ch = str.charAt(i);
            String remaining = str.substring(0, i) + str.substring(i + 1);
            permute(remaining, ans + ch);
        }
    }

    public static void main(String[] args) {
        String str = "ABC";
        permute(str, "");
    }
}

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation:

  • In the first call to permute(), the string "ABC" is given and we fix each character in turn. For each fixed character, we call permute() recursively with the remaining part of the string, producing the permutations.
  • Base case: When str.length() is 0, the function prints the accumulated answer (ans).

2. Backtracking

Backtracking is an extension of recursion that tries to build a solution incrementally. If a solution cannot be completed, it undoes the last choice and tries a different one. The backtracking technique is essentially a depth-first search, where you explore all the possible paths but backtrack when needed.

Backtracking becomes especially efficient when dealing with problems that have constraints (e.g., permutations where certain tasks or characters cannot be placed together). By pruning the search space early and avoiding invalid configurations, backtracking prevents unnecessary work, exploring only feasible solutions.

This can greatly reduce the problem’s complexity when constraints limit the number of valid possibilities. For example, when generating permutations, if a character has already been used or cannot be placed in a specific position, backtracking helps skip those invalid branches, improving performance.

Approach:

  • Explore a character's possible positions.
  • After each recursive call, backtrack by swapping the characters back to their original position.

Example: Permutation Java using Backtracking

public class PermutationBacktracking {

    public static void backtrack(String str, int left, int right) {
        // If all characters are fixed, print the permutation
        if (left == right) {
            System.out.println(str);
            return;
        }

        // Swap each character and recurse
        for (int i = left; i <= right; i++) {
            str = swap(str, left, i);  // Swap characters at position 'left' and 'i'
            backtrack(str, left + 1, right);  // Recur with the next character
            str = swap(str, left, i);  // Backtrack by swapping back
        }
    }

    // Utility function to swap characters in the string
    private static String swap(String str, int i, int j) {
        char temp;
        char[] charArray = str.toCharArray();
        temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }

    public static void main(String[] args) {
        String str = "ABC";
        backtrack(str, 0, str.length() - 1);
    }
}

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation:

  • The function backtrack() recursively generates permutations by fixing a character and swapping it with other characters. After each recursive call, we swap the characters back to explore the next possible combination.
  • Backtracking happens when we swap characters back to their original positions, allowing us to explore new possibilities.

3. Iterative Approach

An iterative approach generates all possible permutations using loops. While recursion is more suitable for this type of problem, an iterative approach is also efficient and can be implemented using algorithms like Heap's algorithm.

Heap's algorithm is a widely-used iterative method for generating permutations. It is more memory-efficient compared to recursion because it avoids the overhead of function calls and stack frames.

Approach:

The algorithm systematically generates the next permutation by swapping elements and moving to the next set of choices.

Example: Iterative Permutation Java Using Heap's Algorithm

public class IterativeHeapAlgorithm {

    public static void generatePermutations(String str) {
        int n = str.length();
        char[] array = str.toCharArray();
        int[] c = new int[n];
        System.out.println(new String(array)); // Print the first permutation

        int i = 0;
        while (i < n) {
            if (c[i] < i) {
                swap(array, i % 2 == 0 ? 0 : c[i], i);
                System.out.println(new String(array));
                c[i]++; // Move to the next configuration
                i = 0;
            } else {
                c[i] = 0;
                i++;
            }
        }
    }

    private static void swap(char[] array, int i, int j) {
        char temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static void main(String[] args) {
        String str = "ABC";
        generatePermutations(str);
    }
}

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation:

  • The function generatePermutations() uses an iterative approach based on Heap’s Algorithm to generate permutations.
  • It uses an array c to track the number of swaps needed for each position and iteratively generates permutations by swapping characters.

Also Read: Iterator in Java: Understanding the Fundamentals of Java Iterator

Technical Depth and Algorithm Analysis

When generating permutations, understanding time and space complexities is essential for handling large datasets effectively. Below is an analysis of the complexities involved in typical recursive and backtracking approaches.

Time Complexity:

For a string of length n, there are n! permutations. Each permutation generation involves O(n) operations (such as swaps). Thus, the overall time complexity is O(n * n!), considering both the generation of permutations and the operations performed on each.

Space Complexity:

Space complexity arises from the recursive call stack and storing the permutations. The call stack depth is O(n), and storing n! permutations requires O(n * n!) space. Therefore, the space complexity is O(n * n!).

Scalability Considerations:

As n increases, the time and space complexities grow rapidly. Optimizations like skipping duplicates early or using iterative methods can improve performance, but the problem remains computationally expensive for large datasets. For practical applications, careful consideration of input size and possible optimizations is crucial.

Now that you've explored the three different approaches to solving the permutation problem, let's understand how these techniques can help you solve permutation in Java.

Permutation of Strings in Java: Explained 

You can generate a permutation of string in Java using different approaches such as recursion, backtracking, and iterative methods. The methods allow you to design algorithms that are efficient and computationally inexpensive.

Here's how the permutation of string in Java is solved using different approaches.

Permutations of Strings Using Recursion

Recursion is a method where a function calls itself to solve a smaller instance of the problem. For string permutations, the logic is to fix one character at a time and recursively generate permutations of the remaining characters. 

The process continues until the string is reduced to a single character, at which point you can directly return the permutation.

Steps involved:

  • Base Case: If the string is of length 1, return the string.
  • Recursive Case: For each character in the string, swap it with the first character and recursively find permutations of the rest of the string.
  • Backtrack: After each recursive call, undo the swap (backtrack) to restore the original string and continue generating other permutations.

Example of implementation using ABC string:

1. Start with "ABC":

Fix the first character "A". Now permute the rest: "BC".

2. Permuting "BC":

Fix "B", permute "C" (only one character, so "BC" is a valid permutation).

Now, backtrack and swap "B" and "C" → "CB".

3. Permuting "CB":

Fix "C", permute "B" (only one character, so "CB" is a valid permutation).

Backtrack: swap "C" and "B" → "BC" (restore original order).

4. Back to "ABC":

Now swap "A" and "B" → "BAC". Permute the remaining "AC".

5. Permuting "AC":

Fix "A", permute "C" → "AC".

Backtrack: swap "A" and "C" → "CA".

6. Permuting "CA":

Fix "C", permute "A" → "CA".

Backtrack: swap "C" and "A" → "AC" (restore original order).

7. Back to "ABC":

Now swap "A" and "C" → "CAB". Permute the remaining "AB".

8. Permuting "AB":

Fix "A", permute "B" → "AB".

Backtrack: swap "A" and "B" → "BA".

9. Permuting "BA":

Fix "B", permute "A" → "BA".

Backtrack: swap "B" and "A" → "AB" (restore original order).

Code Snippet:

import java.util.ArrayList;
import java.util.List;

public class StringPermutations {

    // Function to generate permutations
    public static void permute(String str, int left, int right, List<String> result) {
        // Base case: if left index equals right, add the permutation
        if (left == right) {
            result.add(str);
        } else {
            // Recursive case: swap each character and recurse
            for (int i = left; i <= right; i++) {
                str = swap(str, left, i);  // Swap characters
                permute(str, left + 1, right, result);  // Recurse
                str = swap(str, left, i);  // Backtrack
            }
        }
    }

    // Helper function to swap characters in a string
    public static String swap(String str, int i, int j) {
        char[] charArray = str.toCharArray();
        char temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return new String(charArray);
    }

    public static void main(String[] args) {
        String str = "ABC";
        List<String> result = new ArrayList<>();
        permute(str, 0, str.length() - 1, result);

        // Print all permutations
        for (String perm : result) {
            System.out.println(perm);
        }
    }
}

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation: 

  • Function Definition:
    • permute(String str, int left, int right, List<String> result) is the main function to generate permutations of the string str.
    • Base Case: When left == right, the string has been fully permuted, so it is added to the result list.
    • Recursive Case: If not, the function swaps each character in the string and recursively generates permutations of the remaining part of the string (from left + 1 to right).
    • Backtracking: After the recursion, the characters are swapped back to their original positions to explore other possibilities.
       
  • Helper Function:
    • swap(String str, int i, int j) swaps the characters at positions i and j in the string, returning the modified string.
       
  • Main Execution:
    • A string "ABC" is passed to the permute function, along with an initially empty list result.
    • After calling the permute function, the result list contains all permutations of "ABC", which are printed.

Backtracking Approach to Permutations

In the backtracking approach, you have to build the solution incrementally, exploring each possibility. If a solution turns out to be incorrect, you need to backtrack to a previous step and try a different approach. 

In the context of permutations, backtracking is used to try all possible swaps and generate permutations.

Step involved:

  • Start with the first element: Fix one character and swap it with other characters.
  • Recursion: Recursively swap the remaining characters to generate all possible combinations.
  • Backtrack: If no solution is found or all possibilities are exhausted, undo the swap to explore other possible solutions.

Example of implementation using ABC string:

1. Start with "ABC":

Fix the first character, "A". Now permute the rest: "BC".

2. Permute "BC":

Swap "B" and "B" → "ABC" → Add to result.

Backtrack: Swap "B" and "B" back → "ABC".

3. Permute "CB" (Swap "C" with "B"):

Swap "C" and "B" → "ACB" → Add to result.

Backtrack: Swap "C" and "B" back → "ABC".

4. Backtrack to "ABC":

Swap "B" and "A" → "BAC". Permute the remaining "AC".

5. Permute "AC":

Swap "A" and "A" → "BAC" → Add to result.

Backtrack: Swap "A" and "A" back → "BAC".

6. Permute "CA" (Swap "C" with "A"):

Swap "C" and "A" → "BCA" → Add to result.

Backtrack: Swap "C" and "A" back → "BAC".

7. Backtrack to "ABC":

Swap "C" and "A" → "CAB". Permute the remaining "AB".

8. Permute "AB":

Swap "A" and "A" → "CAB" → Add to result.

Backtrack: Swap "A" and "A" back → "CAB".

9. Permute "BA" (Swap "B" with "A"):

Swap "B" and "A" → "CBA" → Add to result.

Backtrack: Swap "B" and "A" back → "CAB"

Code Snippet:

import java.util.ArrayList;
import java.util.List;

public class BacktrackingPermutations {

    // Function to generate permutations using backtracking
    public static void permuteBacktracking(String str, int start, int end, List<String> result) {
        // Base case: If we have a valid permutation, add it to the result list
        if (start == end) {
            result.add(str);
            return;
        }

        // Recursive case: Try every character in the string
        for (int i = start; i <= end; i++) {
            // Swap characters at positions 'start' and 'i'
            str = swap(str, start, i);
            // Recur for the next position
            permuteBacktracking(str, start + 1, end, result);
            // Backtrack by swapping back
            str = swap(str, start, i);
        }
    }

    // Helper function to swap characters in the string
    public static String swap(String str, int i, int j) {
        char[] charArray = str.toCharArray();
        char temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return new String(charArray);
    }

    public static void main(String[] args) {
        String str = "ABC";
        List<String> result = new ArrayList<>();
        permuteBacktracking(str, 0, str.length() - 1, result);

        // Print all permutations
        for (String perm : result) {
            System.out.println(perm);
        }
    }
}

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation of the Code:

  • Function Definition:
    • permuteBacktracking(String str, int start, int end, List<String> result) is the function that generates permutations using the backtracking approach.
    • Base Case: If start == end, the string is fully permuted and is added to the result list.
    • Recursive Case: For each character in the string, swap it with the character at the start position, and recursively generate permutations for the remaining part of the string.
    • Backtracking: After each recursion, the characters are swapped back to their original positions (undoing the changes) to explore other permutations.
  • Helper Function:
    • swap(String str, int i, int j) swaps characters at positions i and j in the string, then returns the modified string.
  • Main Execution:
    • A string "ABC" is passed to the permuteBacktracking function, along with an initially empty result list.

The permuteBacktracking function is called to generate all permutations of the string "ABC". The results are stored in the result list and then printed out.
 

Iterative Method for String Permutations

The iterative method generates permutations without recursion, typically by iterating over all possible combinations. One common algorithm for this is Heap's algorithm, which can generate permutations iteratively.

Steps in implementation:

  • Sort the string: Start by sorting the string in lexicographical order.
  • Generate permutations: Use loops to swap elements and generate all possible permutations.
  • Stop when no further permutations are possible: Continue swapping until all permutations are generated.

Example of implementation using ABC string:

1. Start with the sorted string "ABC" (lexicographically smallest permutation):

  • Print: "ABC"

2. Find the next permutation:

  • Identify the largest index i such that arr[i] < arr[i + 1]. Here i = 1 (B < C).
  • Find the largest j such that arr[j] > arr[i]. Here j = 2 (C > B).
  • Swap arr[i] and arr[j] → "ACB".
  • Reverse the substring after i (no change needed).
  • Print: "ACB".

3. Find the next permutation:

  • Identify i = 0 (A < C).
  • Find j = 2 (C > A).
  • Swap arr[i] and arr[j] → "CBA".
  • Reverse the substring after i → "BCA".
  • Print: "BCA".

4. Find the next permutation:

  • Identify i = 1 (B < C).
  • Find j = 2 (C > B).
  • Swap arr[i] and arr[j] → "CAB".
  • Reverse the substring after i → "CBA".
  • Print: "CBA".

Code Snippet:

import java.util.Arrays;

public class IterativePermutations {

    // Function to print all permutations of the string using the iterative approach
    public static void printPermutations(String str) {
        // Convert string to character array
        char[] arr = str.toCharArray();
        Arrays.sort(arr); // Sort the array to generate permutations in lexicographical order
        
        while (true) {
            System.out.println(new String(arr));
            int i = arr.length - 1;
            
            // Find the first element that is smaller than its next element
            while (i > 0 && arr[i - 1] >= arr[i]) {
                i--;
            }
            
            if (i <= 0) {
                break; // All permutations have been generated
            }
            
            int j = arr.length - 1;
            // Find the element just larger than arr[i-1]
            while (arr[j] <= arr[i - 1]) {
                j--;
            }
            
            // Swap arr[i-1] and arr[j]
            char temp = arr[i - 1];
            arr[i - 1] = arr[j];
            arr[j] = temp;
            
            // Reverse the elements from i to the end
            j = arr.length - 1;
            while (i < j) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
        }
    }

    public static void main(String[] args) {
        String str = "ABC";
        printPermutations(str);
    }
}

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation:

The printPermutations() method is defined in the IterativePermutations class to generate all permutations of a string using an iterative approach.

  1. Sorting: The string is first converted into a character array and sorted to generate permutations in lexicographical order.
  2. Iterative Process:
    • while (true) loop is used to continuously generate the next permutation.
    • Find Pivot: The first element from the right that is smaller than its next element is identified as the "pivot."
    • Find Successor: The smallest element greater than the pivot is located to swap with it.
    • Swap and Reverse: After the pivot and successor are swapped, the tail portion of the array (elements after the pivot) is reversed to ensure the smallest next permutation.
  3. Termination Condition:
    • The loop terminates when no more valid permutations can be generated, i.e., when the array is fully sorted in descending order.
  4. Runtime Execution:
    • The algorithm dynamically computes the next permutation based on the current state of the array, similar to how Java decides which overridden method to call at runtime using dynamic method dispatch.

Now that you’ve seen how you implement the different approaches to solve permutation of string in Java, let’s implement some examples of permutation code in Java.

Ready to build a strong foundation in Java programming? Enroll in upGrad’s Core Java Basics course today and start your coding journey!

Permutation Code in Java: Step-by-Step Implementation with Examples

Java allows you to solve permutation java problems using either of the three approaches that you have explored in the previous section. However, the recursive approach is considered to be the most suitable to handle strings due to its ability to check all the possible conditions.

Here are three different examples of implementing permutation code in Java.

Example 1: There are six people participating in a skit. In how many ways first and the second prize can be awarded?

In this example, you have to determine how many ways you can award the first and second prize to six people. You have to find the permutations of 6 people taken 2 at a time.

You can use the following formula to solve the problem.

P ( n , r ) = n ! ( n - r ) !

Here, n = 6 (number of people) and r = 2 (prizes to be awarded).

Inserting these values in the formula, you get:

P ( 6 , 2 ) = 6 ! ( 6 - 2 ) ! =   6 · 5 · 4 · 3 · 2 · 1 4 · 3 · 2 · 1 =   30 1 P ( 6 , 2 ) = 30

Implementation in Java:

public class PermutationExample1 {

    // Method to calculate permutations (nPr) of n items taken r at a time
    public static int permutation(int n, int r) {
        return factorial(n) / factorial(n - r);  // P(n, r) = n! / (n - r)!
    }

    // Method to calculate factorial of a number
    public static int factorial(int num) {
        int result = 1;
        for (int i = 1; i <= num; i++) {
            result *= i;
        }
        return result;
    }

    public static void main(String[] args) {
        int n = 6;  // Total number of participants
        int r = 2;  // Number of prizes to be awarded

        // Calculate and display the number of permutations (ways to award prizes)
        int result = permutation(n, r);
        System.out.println("Total ways to award first and second prize: " + result);
    }
}

Output:

Total ways to award first and second prize: 30

Explanation:

The Example class calculates the number of permutations (nPr) of n items taken r at a time, which is often used to determine the number of ways to award prizes in a competition.

  1. Permutation Method:
    • The permutation(int n, int r) method calculates the number of permutations using the formula P(n, r) = n! / (n - r)!.
    • This formula gives the number of ways to choose r items from n items when the order of selection matters.
  2. Factorial Method:
    • The factorial(int num) method calculates the factorial of a given number num. The factorial is calculated by multiplying all integers from 1 to num (i.e., num! = num * (num - 1) * ... * 1).
  3. Main Execution:
    • In the main() method, n is set to 6 (total participants) and r is set to 2 (prizes to be awarded).
    • The permutation(n, r) method is called to calculate the number of ways to award the first and second prizes.
    • The result is printed out, which represents the total number of ways to award two prizes from six participants.

Also Read: 50 Java Projects With Source Code in 2025: From Beginner to Advanced

Example 2: Find the permutation of a number n greater than the number itself.

In this example, let's assume you are given a number n (for example, 3), and you want to find the permutations of a number greater than n (for instance, 5). This means we are you have to calculate the permutations of 5 items taken 3 at a time.

Using the permutation formula, you get the following:

P ( 5 , 3 ) = 5 ! ( 5 - 3 ) ! =   5 · 4 · 3 · 2 · 1 2 · 1 =   60 P ( 6 , 2 ) = 30

Implementation using Java:

public class PermutationExample2 {

    // Method to calculate permutations (nPr)
    public static int permutation(int n, int r) {
        return factorial(n) / factorial(n - r);
    }

    // Method to calculate factorial
    public static int factorial(int num) {
        int result = 1;
        for (int i = 1; i <= num; i++) {
            result *= i;
        }
        return result;
    }

    public static void main(String[] args) {
        int n = 5;  // Total number of items
        int r = 3;  // Number of items to choose

        // Calculate and display the number of permutations
        int result = permutation(n, r);
        System.out.println("Total permutations of " + n + " items taken " + r + " at a time: " + result);
    }
}

Output:

Total permutations of 5 items taken 3 at a time: 60

Explanation:

The PermutationExample2 class calculates the number of permutations (nPr) of n items taken r at a time, which is useful for determining how many ways a subset of items can be chosen when the order matters.

  1. Permutation Method:
    • The permutation(int n, int r) method calculates the number of permutations using the formula P(n, r) = n! / (n - r)!.
    • This formula is used to find the number of ways to choose r items from n items where the order of selection is important.
  2. Factorial Method:
    • The factorial(int num) method calculates the factorial of a given number num. The factorial is calculated by multiplying all integers from 1 to num (i.e., num! = num * (num - 1) * ... * 1).
  3. Main Execution:
    • In the main() method, n is set to 5 (total items available) and r is set to 3 (number of items to be chosen).
    • The permutation(n, r) method is called to calculate the number of ways to select 3 items from 5, taking the order into account.
    • The result is printed out, which represents the total number of permutations for choosing 3 items from 5.

Example 3: Find all the permutations of a string in Java

In this example, you will have to find all possible permutations of the string "ABC" using recursion. This is a classic permutation problem where you have to swap characters and recurse to explore all possible combinations. The logic of solving permutation in Java using recursion has already been discussed.

Implementation using Java:

import java.util.ArrayList;
import java.util.List;

public class StringPermutations {

    // Method to generate all permutations of a string using recursion
    public static void permute(String str, int left, int right, List<String> result) {
        if (left == right) {
            result.add(str);  // Add the permutation to the result
        } else {
            for (int i = left; i <= right; i++) {
                str = swap(str, left, i);  // Swap characters
                permute(str, left + 1, right, result);  // Recurse for the next position
                str = swap(str, left, i);  // Backtrack (restore the string)
            }
        }
    }

    // Helper method to swap characters in the string
    private static String swap(String str, int i, int j) {
        char[] arr = str.toCharArray();
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return new String(arr);
    }

    public static void main(String[] args) {
        String str = "ABC";  // The string for which we need to find permutations
        List<String> result = new ArrayList<>();  // List to store permutations

        // Call the permute method
        permute(str, 0, str.length() - 1, result);

        // Display the result
        System.out.println("All permutations of the string \"" + str + "\":");
        for (String permutation : result) {
            System.out.println(permutation);
        }
    }
}

Output:

All permutations of the string "ABC":
ABC
ACB
BAC
BCA
CAB
CBA

Explanation:

The StringPermutations class generates all permutations of a string using a recursive backtracking approach.

  1. permute Java Method:
    • The permute(String str, int left, int right, List<String> result) method generates all permutations of the given string str.
    • Base Case: If left == right, the string has been fully permuted and is added to the result list.
    • Recursive Case:
      • The method iterates over the string starting from left to right.
      • It swaps the character at the current index i with the character at left, then recursively calls the permute method to generate permutations for the next position (left + 1).
      • After the recursive call, the string is restored to its original state using backtracking (by swapping the characters back).
  2. swap Method:
    • The swap(String str, int i, int j) method swaps the characters at indices i and j in the string.
    • It converts the string to a character array, performs the swap, and returns the modified string.
  3. Main Execution:
    • The main() method defines the string str = "ABC", which is the input for generating permutations.
    • An empty list result is created to store the permutations.
    • The permute method is called with the initial parameters (left = 0right = str.length() - 1), and the permutations are added to the result list.
    • Finally, the program prints all the generated permutations of the string "ABC".

Also Read: Top 135+ Java Interview Questions You Should Know in 2025

Although you've implemented the permutation code in Java, it can be optimized for better memory and time efficiency.

Here’s how you can optimize your code.

1. Avoid Redundant Calculations:

  • In recursive algorithms, avoid recalculating the same sub-problems multiple times. For instance, in permutation or factorial problems, calculations can often be repeated across different branches of the recursion. To address this, you can store intermediate results (e.g., computed factorials or partial permutations) in a data structure like a hash map or an array. This reduces the computational overhead and improves the performance significantly.
  • Example: If you're calculating factorials in a permutation problem, store each result in a memoization table so you don’t compute factorials for the same numbers multiple times.

2. Memoization:

  • Memoization is an optimization technique where results of expensive function calls (like calculating permutations or factorials) are stored in a cache (e.g., a hash map or an array) so that future calls with the same inputs can return the stored result, instead of recomputing it.
  • Use cases: This is especially useful when there are repeated calculations that involve the same input values, as in recursive approaches to generating permutations or solving dynamic programming problems.
  • Example: If you're recursively calculating permutations and need factorial values, you can cache the results of factorial calculations and avoid recomputing them in each recursive step.

3. Iterative Solution:

  • While recursion is often the most intuitive approach to problems like permutation generation, it can be inefficient and harder to manage for larger inputs due to its space complexity (stack frames for each recursive call). An iterative approach can often be more efficient for problems that don't necessarily require recursion.
  • Iterative algorithms also tend to be easier to understand and debug compared to their recursive counterparts, especially for problems where you simply need to generate permutations in lexicographical order or follow a predictable sequence of operations.
  • Example: Instead of recursively generating all permutations, you can use an iterative method such as next permutation algorithm, which generates permutations one by one without recursion.

4. StringBuilder for Swapping:

  • StringBuilder is more efficient than using regular String when it comes to modifying characters, as Strings are immutable in Java. Every time you modify a String, a new String object is created, which can be quite costly in terms of both time and memory.
  • StringBuilder, on the other hand, allows for mutable string manipulation, meaning you can modify the contents of the StringBuilder without creating new objects each time. This makes swapping characters in permutations more efficient.
  • Example: In a permutation algorithm, instead of converting a String to an array and swapping elements back and forth, you could convert the String to a StringBuilder, swap characters, and avoid creating unnecessary new objects.

5. Prune Unnecessary Branches:

  • In a backtracking approach, you often explore all possible solutions by recursively exploring every path in the problem space. However, not all branches are relevant, and exploring them may waste computational resources.
  • Pruning refers to cutting off paths that are unlikely to lead to a valid solution. By applying pruning, you reduce the size of the search space and improve performance. For example, when generating permutations, if you encounter a duplicate or an already processed arrangement, you can skip processing that branch to avoid redundant work.
  • Example: In a permutation problem, once you have used a character, you can skip it in subsequent recursive calls to avoid generating duplicate permutations. Similarly, if a partial permutation is no longer valid (e.g., violates constraints), you can prune that path early to save time.

Now that you’ve looked at some tips to optimize your code for permutation in Java, let’s explore applications of permutation in the practical scenarios. 

Practical Applications of Permutations in Real Scenarios

Permutations are important in real-world scenarios where the arrangement of elements plays a critical role in decision-making or optimization. From cryptography in cybersecurity to gaming, permutations help you solve complex problems by exploring all possible arrangements or combinations.

Here are the applications of permutation in real life.

Where Permutations Are Used in Real Life?

Applications of permutations in real life range from data encryption and password generation to gaming and optimization. Here are its uses in real-life scenarios.

Use Case

Description

Example

Data Encryption Encryption algorithms like AES (Advanced Encryption Standard) and RSA (Rivest-Shamir-Adleman) use permutations to shuffle and obscure data, making it unreadable to unauthorized parties. AES (Advanced Encryption Standard): AES uses permutations along with substitution and transposition techniques to encrypt data, ensuring that the encrypted data is unreadable without the correct key.
Password Generation Permutations help generate secure passwords by creating a variety of possible combinations of characters, numbers, and symbols. This ensures that passwords are difficult to guess or brute-force. Random Password Generation: A secure password like "a7!X$y2p" is created by generating all possible combinations of characters, numbers, and symbols from a defined character set, ensuring complexity and security.
Captcha Systems Permutations of letters, numbers, and symbols are used in CAPTCHA challenges to distinguish between humans and bots, ensuring only humans can pass the verification process. Google CAPTCHA: A typical CAPTCHA might show an image with randomized letters and numbers (e.g., "9w3c8l"), requiring users to enter the correct permutation of the characters to prove they're human.
Security Tokens In two-factor authentication (2FA) systems, permutations are used to generate random security tokens or one-time passwords (OTPs) that are difficult for attackers to guess or predict. Google Authenticator: The app generates time-based security tokens (e.g., "572489") by permuting a shared secret key and the current time, ensuring that each token is unique and valid only for a short time.
Cryptographic Hash Functions Hash functions like SHA-256 use permutations to scramble input data in cybersecurity (like passwords or files) into fixed-size outputs (hashes) that act as a unique fingerprint for the original input. SHA-256: When a password like "myPassword123" is hashed, SHA-256 applies multiple rounds of permutations and transformations to produce a fixed-length hash (e.g., "f5e5e6d2b7c5f98..."), which is unique to the input.
Random Sampling and Lottery Systems Permutations are used to select random winners in lottery systems or to choose randomized samples from large datasets, ensuring fairness and unpredictability. Lottery Draws: In a lottery, the winning ticket is chosen by permuting all possible ticket numbers, ensuring that each ticket has an equal chance of being selected. For example, a permutation of tickets 1-100 could randomly select the winning ticket.
Scheduling and Optimization Permutations are used in optimization problems like scheduling tasks or finding the best route (e.g., traveling salesman problem) by exploring all possible arrangements and selecting the optimal one. Job Scheduling: In task scheduling systems, permutations of tasks (e.g., tasks A, B, C) are examined to find the most efficient order of execution that minimizes waiting times or resource conflicts.
Combinatorial Designs Permutations are used in combinatorial designs to arrange items in a specific order or pattern, often to test hypotheses, optimize experiments, or design efficient systems. Experimental Design: In agricultural experiments, permutations of different crop varieties, soil types, and irrigation levels are tested to identify the best combination for optimal growth.

Also Read: What is DES (Data Encryption Standard)? DES Algorithm Explained

While permutations have wider applications in the real world, they are specifically preferred in the software development process for purposes like game design and optimization. Let’s look at the applications in detail.

Applications in Software Development

Permutations are used extensively for optimizing algorithms, improving game design, and enhancing simulations.

Here are the applications of permutations in software development.

Use Case

Description

Example

Simulations Permutations are used in simulations to test various input combinations and predict outcomes, such as in weather or financial models. Weather Modeling: Testing different combinations of temperature, pressure, and wind speed to predict weather patterns.
Gaming Permutations generate random sequences or configurations in games (card, board, puzzle games) to ensure variety and unpredictability.

Poker: Shuffling cards using permutations for random distribution. 

Sudoku: Creating unique and solvable puzzles by permuting numbers on the grid.

Combinatorial Optimization Used in optimization problems to find the best arrangement of items (e.g., shortest route, minimal cost). Traveling Salesman Problem (TSP): Finding the shortest route by permuting cities in different orders.
Machine Learning In hyperparameter optimization, permutations of different parameter combinations are tested to fine-tune models for better performance. Hyperparameter Tuning: Testing different combinations of learning rate, batch size, etc., to optimize model accuracy.
Genetic Algorithms Permutations of genetic sequences help evolve optimal solutions in problems like scheduling or resource allocation. Job Scheduling: Using permutations of task orders in genetic algorithms to find the most efficient scheduling sequence.

Now that you’ve explored the applications of permutations in the real world and software development, let’s explore some advanced concepts related to this topic.

Exploring Advanced Topics in Permutations

Advanced concepts in permutations will be useful while dealing with large data sets, duplicate elements, or optimizing performance. With proper permutations, you can ensure that algorithms scale well with increasing data size.

Let us have a look at each of these one by one, starting with how to tackle duplicate characters in permutations.

Handling Duplicate Characters in Permutations

When dealing with permutations of strings containing repeated characters, generating duplicate results can be a major issue. 

Without addressing this, the number of permutations can increase exponentially, leading to redundant computations. 

Here are the techniques to handle duplicate characters.

1. Sorting the Array

  • Why it Helps: Sorting ensures that duplicate characters are grouped together, allowing us to skip over them easily.
  • How it Works: After sorting, we process characters in a unique order and skip duplicates at each recursion level to avoid generating identical permutations.

Example:

For the string "AAB", sorting results in ["A", "A", "B"]. After placing one "A", the next "A" is skipped.

Code:

Arrays.sort(arr); // Sort to handle duplicates

2. Backtracking

  • Why it Helps: Backtracking allows us to build permutations incrementally and undo choices if needed, while skipping over duplicates.
  • How it Works: During permutation generation, we skip characters that are the same as the previous one at the current recursion level.

Example:

In the string "AAB", once the first "A" is used, we skip the second "A" to avoid duplicate permutations.

Code:

if (i > left && arr[i] == arr[i - 1]) continue; // Skip duplicates

3. Hashing or Set Data Structures

  • Why it Helps: A hash set automatically handles duplicates, ensuring only unique permutations are stored.
  • How it Works: After generating a permutation, convert it to a string and add it to a hash set. If it’s a duplicate, it won’t be added.

Example:

For "AAB", the permutations are "AAB""ABA", and "BAA". Using a hash set ensures only unique permutations are kept.

Code:

Set<String> resultSet = new HashSet<>();
resultSet.add(permutation); // Automatically handles duplicates

Addressing Edge Cases

Edge cases are unique situations in which the input or problem deviates from the general pattern, potentially causing errors or inefficiencies if not handled correctly. These cases are often extreme or rare, such as an empty input, a string with only one character, or repeated characters. 

Properly addressing edge cases ensures that the algorithm behaves as expected and doesn't run into issues like infinite loops, redundant calculations, or incorrect results.

When generating permutations, it’s important to explicitly address duplicates and edge cases to ensure the algorithm functions as expected under all conditions. Without handling these situations, the algorithm might encounter inefficiencies, incorrect results, or errors. Below are some common edge cases and strategies for handling them.

1. All Characters Are the Same:

  • Problem: If the string contains only one repeated character (e.g., "AAAA"), every permutation will be identical. This leads to unnecessary calculations and redundant results.

Solution: Early in the algorithm, check if all characters are the same and immediately return the single possible permutation to save processing time.

public List<String> generatePermutations(String str) {
	if (str == null || str.length() == 0) {
    	return Collections.singletonList("");
	}

	if (str.chars().allMatch(c -> c == str.charAt(0))) {
    	return Collections.singletonList(str);  // Return only the original string if all characters are identical
	}

	// Proceed with regular permutation generation if not all characters are the same
	// ...
}

2. Empty String:

  • Problem: An empty string has no characters, so technically, there are no permutations. Generating permutations for an empty string could lead to errors or unnecessary processing.

Solution: If the input string is empty, directly return a set containing only the empty string as the only possible permutation.

public List<String> generatePermutations(String str) {
	if (str == null || str.length() == 0) {
    	return Collections.singletonList("");  // Return empty string as the only permutation
	}

	// Proceed with regular permutation generation if the string is not empty
	// ...
}

3. Single Character String:

  • Problem: A string with just one character (e.g., "A") has only one permutation, which is the string itself. Without handling this case, the algorithm might process it like any other string, wasting resources.

Solution: If the string has only one character, immediately return that string as the only permutation, bypassing unnecessary computation.

public List<String> generatePermutations(String str) {
	if (str == null || str.length() == 0) {
    	return Collections.singletonList("");  // Return empty string as the only permutation
	}

	if (str.length() == 1) {
    	return Collections.singletonList(str);  // Return the string itself as the only permutation
	}

	// Proceed with regular permutation generation if string length is greater than 1
	// ...
}

4. Handling Duplicates in General

  • Problem: If the input string contains duplicate characters, the standard permutation algorithm may produce redundant results. This can lead to inefficient memory use and unnecessary computation.
  • Solution: One effective approach is to use a set to store the permutations, which will automatically handle duplicate entries. Additionally, you can sort the string at the beginning of the algorithm and skip generating permutations that are the same as previously encountered.

Example Code:

public Set<String> generatePermutations(String str) {
	Set<String> permutations = new HashSet<>();
	if (str == null || str.length() == 0) {
    	permutations.add("");  // Return empty string as the only permutation
    	return permutations;
	}

	// Sort the string to ensure duplicate permutations are avoided
	char[] chars = str.toCharArray();
	Arrays.sort(chars);
	generatePermutationsHelper(chars, 0, permutations);
	return permutations;
}

private void generatePermutationsHelper(char[] chars, int index, Set<String> permutations) {
	if (index == chars.length) {
    	permutations.add(new String(chars));  // Add the permutation to the set
    	return;
	}

	for (int i = index; i < chars.length; i++) {
    	if (i > index && chars[i] == chars[index]) continue;  // Skip duplicates
    	swap(chars, i, index);
    	generatePermutationsHelper(chars, index + 1, permutations);
    	swap(chars, i, index);  // Backtrack
	}
}

private void swap(char[] chars, int i, int j) {
	char temp = chars[i];
	chars[i] = chars[j];
	chars[j] = temp;
}

Now that you know how to handle duplicate characters in permutations, let’s explore different ways to handle large datasets.

Permutations for Large Datasets

Handling permutations for large data sets needs careful optimization to ensure that performance is not affected as the data size increases. 

Here’s how you can handle large datasets.

  • Memoization: Store already computed permutations to avoid recalculating them repeatedly, thus improving time complexity.
  • Iterative Methods: Consider using iterative methods to reduce memory overhead and improve efficiency for large data sets.
  • Dynamic Programming: By breaking down the problem into subproblems, dynamic programming can generate permutations more efficiently.

Now that you’ve learned how to handle challenges like large datasets and duplicate characters, it's important to choose the right approach for solving permutation in Java. Let's compare the three techniques.

Comparison of Different Permutation Methods

To choose the best approach to solve permutation in Java, you need to consider factors like time and memory consumption.

Here’s the difference between the three approaches to solving permutation in Java.

Method

Approach

Advantages

Disadvantages

Recursive Uses a divide-and-conquer approach, where the problem is broken down into smaller sub-problems.

- Easy to implement.

- Intuitive and straightforward for small datasets.-

 Simple to understand.

- High memory usage due to deep recursion.

- Slower for large datasets due to repeated function calls and stack overhead.

- Risk of stack overflow with very large inputs.

Iterative Uses loops and iterative constructs to generate permutations, typically without recursion.

- Memory efficient for large datasets.

- Avoids deep recursion, reducing stack overhead.

- Can be more efficient in some cases.

- More complex to implement than recursion.

- Less intuitive and harder to understand for beginners.

- May require more code for managing permutation generation.

Library-Based Utilizes built-in functions (e.g., Collections.permute() or Arrays methods) to generate permutations.

- Simplifies implementation.

- Built-in error handling.

- Less code to write and test.

- Saves time in development.

- Less flexibility for customization.

- Dependent on library capabilities.

- Limited for learning about the underlying algorithm.

- May not be suitable for highly specific or customized use cases.

Choosing the Right Method

  • For small datasets or when simplicity is key: The recursive approach is a great choice because it's easy to implement and intuitive. However, for larger datasets, be cautious of memory overhead.
  • For large datasets or memory efficiency: The iterative approach is often the best option because it avoids recursion and minimizes memory usage. It is more complex but highly efficient.
  • For quick development or standard use cases: The library-based approach is perfect, especially if performance isn’t a critical concern, and you need a fast and simple solution without writing much code.

The comparison between different approaches will help you choose the best one for implementation. However, you may encounter challenges while implementing permutation in Java. 

Let’s explore some tips to handle these errors effectively.

Avoiding Common Errors and Troubleshooting in Permutations

When implementing permutation algorithms, especially recursive, you may encounter problems related to infinite loops and missed edge cases. By understanding these challenges and solutions, you can ensure the correctness of your permutation code.

Here are some common errors and tips to avoid them.

Error Type

Description

Tips to Avoid Errors

1. Debugging for Recursive Methods Debugging recursion can be difficult due to base case issues or unclear recursion flow.

Trace Recursion Calls: Use print statements or breakpoints to track recursion flow. 

Check Base Case: Ensure the base case stops recursion correctly. 

Limit Recursion Depth: Use iterative methods for large datasets.

2. Handling Stack Overflow Errors Stack overflow occurs when recursion depth exceeds the system's limit.

Optimize Recursion: Use tail recursion or switch to iterative methods. 

Increase Stack Size: Use -Xss to adjust stack size. 

Limit Recursive Calls: Ensure recursion is necessary for large datasets.

3. Mistakes in Recursion Implementation Issues arise from infinite loops, incorrect termination, or handling duplicates incorrectly.

Avoid Infinite Loops: Ensure recursion moves toward the base case to avoid infinite loops.

Check Termination Condition: Correctly define the base case. 

Handle Duplicates: Skip repeated elements during recursion.

4. Common Logical Errors Errors in recursion structure or missing edge cases can lead to incorrect results.

Correct Recursive Calls: Ensure calls are placed in the right order. 

Account for Edge Cases: Handle empty strings, null values, etc. 

- Data Structures: Ensure proper handling of arrays, lists, and sets.

5. Off-by-One Errors Errors when boundaries of loops or recursion are incorrect, leading to out-of-bound access or missed elements.

Define Loops Correctly: Ensure recursion or loop boundaries do not exceed array/string length. 

Test with Different Inputs: Validate with varied test cases.

6. Inefficient Space Usage Inefficient space usage can cause high memory consumption, especially with large datasets in recursion.

Use In-place Modifications: Modify data in place rather than creating new structures. 

Optimize Recursion: Use tail recursion to reduce stack size.

7. Incorrect Handling of Base Case The base case may be poorly defined, causing infinite recursion or incorrect results.

Check Base Case: Define a clear and correct base case. 

Test with Edge Cases: Always verify edge cases like empty input.

Here are some common problems you may face while implementing permutation in Java. To avoid these issues, it's important to deepen your understanding of Java. Let’s explore how to do that with the help of upGrad.

How Can upGrad Help You Advance Your Career in Java Development?

The application of permutation in Java will help you solve complex problems in fields like cybersecurity and memory management. Understanding how to implement permutations, along with related techniques like backtracking and dynamic programming, will improve your ability to address actual computational challenges.

To deepen your understanding of Java, you can explore specialized courses from upGrad. These courses will help you build a strong foundation and advance your skills for professional growth in software development.

Here are some courses that will boost your programming language skills.

Feeling unsure about where to begin with your programming career? Connect with upGrad’s expert counselors or visit your nearest upGrad offline centre to explore a learning plan tailored to your goals. Transform your programming journey today with upGrad!

Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.

Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.

Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.

Reference:
https://www.netguru.com/blog/is-java-still-used-in-2025

Frequently Asked Questions (FAQs)

1. What is the Best Method to Generate Permutation in Java?

2. Can I Use Java Libraries for Permutations?

3. How to Generate Permutations of Numbers or Other Data Types?

4. What is the easiest way to generate permutation in Java?

5. How can I generate permutations of a string in Java?

6. How do I avoid duplicate permutations for strings with repeating characters?

7. What is the difference between permutations and combinations in Java?

8. What is a recursive approach to generating permutations?

9. What is backtracking, and how is it used for permutation in Java?

10. What are the performance concerns when generating large permutations?

11. How do I handle edge cases in permutation generation?

Rohan Vats

408 articles published

Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months