Home > Java > javaTutorial > body text

How to Generate All Unique Combinations from Multiple Lists of Varying Lengths in Java?

Barbara Streisand
Release: 2024-10-27 13:34:29
Original
269 people have browsed it

How to Generate All Unique Combinations from Multiple Lists of Varying Lengths in Java?

How to Generate All Combinations from Multiple Lists

Question:

Given a variable number of lists with varying lengths, determine how to generate a single list containing all unique combinations of elements from the input lists.

For instance:

Consider the following lists:

X: [A, B, C]
Y: [W, X, Y, Z]
Copy after login

Desired output:

12 unique combinations:

[AW, AX, AY, AZ, BW, BX, BY, BZ, CW, CX, CY, CZ]
Copy after login

Java Implementation:

Using Recursion:

<code class="java">import java.util.List;

public class Permutations {

    public static void main(String[] args) {
        List<List<Character>> lists = List.of(List.of('A', 'B', 'C'), List.of('W', 'X', 'Y', 'Z'));
        List<String> result = new ArrayList<>();
        generatePermutations(lists, result, 0, "");
        System.out.println(result);
    }

    private static void generatePermutations(List<List<Character>> lists, List<String> result, int depth, String current) {
        if (depth == lists.size()) {
            result.add(current);
            return;
        }

        for (int i = 0; i < lists.get(depth).size(); i++) {
            generatePermutations(lists, result, depth + 1, current + lists.get(depth).get(i));
        }
    }
}</code>
Copy after login

How it Works:

This function uses recursion to explore all possible combinations. It maintains a current string representing the partial combination so far and increments the depth to indicate which list is being examined. The base case occurs when all lists have been exhausted, at which point the current partial combination is added to the result list. Otherwise, the function iterates through the current list and recursively calls itself, adding each character to the current combination and advancing the depth.

Example Usage:

<code class="java">Input: lists = [[A, B, C], [W, X, Y, Z]]

Output: [AW, AX, AY, AZ, BW, BX, BY, BZ, CW, CX, CY, CZ]</code>
Copy after login

The above is the detailed content of How to Generate All Unique Combinations from Multiple Lists of Varying Lengths in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!