DEV Community

Sai Kathiroli
Sai Kathiroli

Posted on

Solving the Permutations Problem using Backtracking in Java

Introduction:
The "Permutations" problem on LeetCode challenges us to generate all possible permutations of a given set of distinct integers. In this article, we'll explore an effective solution to this problem using backtracking in Java.

Understanding the Problem:
The goal is to generate all possible permutations of the given array of integers. A permutation is an arrangement of all the elements in a set. The key here is that all permutations should be unique.

Approach:
We will use a backtracking approach to explore all possible combinations and construct permutations. Backtracking is a systematic way of exploring all possible solutions to a problem and undoing a choice if it doesn't lead to a valid solution.

Code Walkthrough:
Let's dive into the provided Java code and understand how it works.

class Solution {
    List<List<Integer>> ass = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        List<Integer> one = new ArrayList<>(nums.length);
        for(int x:nums) {
            one.add(Integer.MIN_VALUE);
        }
        permu(nums, 0, nums.length, one);
        return ass;
    }

    public void permu(int[] nums, int a, int e, List<Integer> was) {
        if(a == e) {
            ass.add(was);
            return;
        }
        for(int i = 0; i < nums.length; i++) {
            if(was.get(i) == Integer.MIN_VALUE) {
                List<Integer> monk = new ArrayList<>(was);
                monk.set(i, nums[a]);
                permu(nums, a + 1, e, monk);
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

List Initialization: The ass list is used to store the final result, i.e., all permutations.

permute() Method: This method initializes a list one with a placeholder value (Integer.MIN_VALUE) and then calls the permu() method to start the permutation generation process.

permu() Method: This is the recursive backtracking method. It checks if the current permutation is complete (a == e) and adds it to the result list. Otherwise, it iterates through the elements of the input array, makes a choice, and recurses.

Improvements and Explanation:
While the provided code successfully generates permutations, there is an issue with the way lists are handled. In Java, when you add a list to another list (ass.add(was)), you are essentially adding a reference to the same list. This can lead to unintended behavior, as modifications to one list will affect others.

To fix this, we should create a deep copy of the list before adding it to the result. Replace the line ass.add(was) with ass.add(new ArrayList<>(was)) to ensure that each permutation is stored independently.

ass.add(new ArrayList<>(was));
Enter fullscreen mode Exit fullscreen mode

Conclusion:
In this article, we explored a backtracking approach to solving the "Permutations" problem on LeetCode using Java. We analyzed the provided code, explained the backtracking strategy, and suggested an improvement to handle list references correctly. Understanding and implementing backtracking is a valuable skill for solving a wide range of combinatorial problems.

Top comments (0)