首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有一个非递归的解决方案来获得所有可能的字符串数组连接的列表?

有没有一个非递归的解决方案来获得所有可能的字符串数组连接的列表?
EN

Stack Overflow用户
提问于 2020-02-17 01:11:48
回答 1查看 97关注 0票数 0

我接收一个字符串数组作为输入。

我必须创建一个字符串列表,这些字符串是输入数组中所有字符串的串联,每个字符串只出现一次。

例如,如果输入数组为["aa","bb","cc"],则得到的字符串列表将为"aabbcc",aaccbb","bbaacc","bbccaa",ccaabb","ccbbaa"

输入数组的长度未知,可以是任何数字。

数组中所有子字符串的长度都相同。

我看到了这个Java: get all concatenations of List问题。

但是有没有什么非递归的解决方案呢?

EN

回答 1

Stack Overflow用户

发布于 2020-02-17 02:02:20

我刚刚写了一个对整数做同样处理的例子:Get all combinations for arbitrary number of elements

你可以在那里简单地用String[]替换int[]

一个完整的交互式示例:

代码语言:javascript
运行
复制
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

class Main
{

    /**
     * Return all possible combination of n Strings
     * @param n Number of parts to combine in each row
     * @param set Array of Strings to combine
     */
    static List<String[]> getAll(int n, String[] set)
    {
        List<String[]> combinations = new ArrayList<>();

        // First step (0)
        // Create initial combinations, filled with the first String.
        for (int number = 0; number < set.length; number++)
        {
            String[] array = new String[n]; // the final size of each array is already known
            array[0] = set[number]; // fill the first Part
            combinations.add(array);
        }

        // In each following step, we add one number to each combination
        for (int step = 1; step < n; step++)
        {
            // Backup the size because we do not want to process
            // the new items that are added by the following loop itself.
            int size = combinations.size();

            // Add one number to the existing combinations
            for (int combination = 0; combination < size; combination++)
            {
                // Add one part to the existing array
                String[] array = combinations.get(combination);
                array[step] = set[0];

                // For all additional Strings, create a copy of the array
                for (int number = 1; number < set.length; number++)
                {
                    String[] copy = Arrays.copyOf(array, array.length);
                    copy[step] = set[number];
                    combinations.add(copy);
                }
            }
        }

        return combinations;
    }

    public static void main(String[] args)
    {
        System.out.println("Enter some Strings, delimited by space");
        Scanner in=new Scanner(System.in);
        String line=in.nextLine();
        String[] set=line.split("\\s+");

        // Calculate all possible combinations
        List<String[]> combinations = getAll(set.length, set);

        // Print the result
        for (String[] combination : combinations)
        {
            System.out.println(Arrays.toString(combination));
        }
    }
}

输出:

代码语言:javascript
运行
复制
Enter some Strings, delimited by space
aa bb cc
[aa, aa, aa]
[bb, aa, aa]
[cc, aa, aa]
[aa, bb, aa]
[aa, cc, aa]
[bb, bb, aa]
[bb, cc, aa]
[cc, bb, aa]
[cc, cc, aa]
[aa, aa, bb]
[aa, aa, cc]
[bb, aa, bb]
[bb, aa, cc]
[cc, aa, bb]
[cc, aa, cc]
[aa, bb, bb]
[aa, bb, cc]
[aa, cc, bb]
[aa, cc, cc]
[bb, bb, bb]
[bb, bb, cc]
[bb, cc, bb]
[bb, cc, cc]
[cc, bb, bb]
[cc, bb, cc]
[cc, cc, bb]
[cc, cc, cc]

要更改输出格式,您可以使用:

代码语言:javascript
运行
复制
        // Print the result
        for (String[] combination : combinations)
        {
            String s=String.join("",combination); // Concatenate the parts of each combination
            System.out.println(s);
        }

输出格式为:

代码语言:javascript
运行
复制
aaaaaa
bbaaaa
ccaaaa
aabbaa
...

请查看链接主题以找到其工作原理的解释。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60251019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档