Java 是否有一个非递归的解决方案来获取字符串数组的所有可能串联的列表?

Java 是否有一个非递归的解决方案来获取字符串数组的所有可能串联的列表?,java,concatenation,Java,Concatenation,我接收一个字符串数组作为输入。 我必须创建一个字符串列表,该列表是输入数组中所有字符串的串联,每个字符串只出现一次。 例如,如果输入数组是[“aa”、“bb”、“cc”],则结果字符串列表将是“aabbcc”、“aaccbb”、“bbaacc”、“bbccaa”、“ccaabb”、“ccbbaa” 输入数组的长度未知,可以是任意数字。 数组中所有子字符串的长度相同。 我看到了这个问题。 但是有什么非递归的解决方案吗?我刚刚写了一个例子,对整数也有同样的效果: 您只需将int[]替换为String

我接收一个字符串数组作为输入。
我必须创建一个字符串列表,该列表是输入数组中所有字符串的串联,每个字符串只出现一次。
例如,如果输入数组是
[“aa”、“bb”、“cc”]
,则结果字符串列表将是
“aabbcc”、“aaccbb”、“bbaacc”、“bbccaa”、“ccaabb”、“ccbbaa”

输入数组的长度未知,可以是任意数字。
数组中所有子字符串的长度相同。
我看到了这个问题。

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

我刚刚写了一个例子,对整数也有同样的效果:

您只需将
int[]
替换为
String[]

完整的交互式示例:

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));
        }
    }
}
要更改输出格式,可以使用:

        // Print the result
        for (String[] combination : combinations)
        {
            String s=String.join("",combination); // Concatenate the parts of each combination
            System.out.println(s);
        }
输出格式为:

aaaaaa
bbaaaa
ccaaaa
aabbaa
...

请查看链接的主题,以了解其工作原理

我刚刚写了一个与整数相同的示例:

您只需将
int[]
替换为
String[]

完整的交互式示例:

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));
        }
    }
}
要更改输出格式,可以使用:

        // Print the result
        for (String[] combination : combinations)
        {
            String s=String.join("",combination); // Concatenate the parts of each combination
            System.out.println(s);
        }
输出格式为:

aaaaaa
bbaaaa
ccaaaa
aabbaa
...

请查看链接的主题,以了解其工作原理

任何可以用递归编写的解决方案都可以不用递归编写。如果您知道递归解决方案,可以通过添加
堆栈
对象来模拟递归使用的调用堆栈,从而使其适应命令式循环。它可能(事实上,肯定不会)没有递归方法那么漂亮,但它会工作的。值得注意的是,这只是模拟递归。大多数人会认为这样的东西仍然是递归的,因为方法本身仍然是递归的。请参阅:任何可以用递归编写的解决方案都可以不用写。如果您知道递归解决方案,可以通过添加
堆栈
对象来模拟递归使用的调用堆栈,从而使其适应命令式循环。它可能(事实上,肯定不会)没有递归方法那么漂亮,但它会工作的。值得注意的是,这只是模拟递归。大多数人会认为这样的事情仍然是递归的,因为方法本身仍然是递归的。请参阅:谢谢你的回答,但是在这里数组中的字符串的数量现在已经提前知道了。我的意思是,它是一个参数,随着运行而变化。因此,我不能像您的代码中那样对循环使用硬编码的
n
。当然,您可以动态填充输入参数。没有必要对它们进行硬编码。这只是一个简单的例子,它关注的是算法,而不是输入和输出。到那时你应该知道长度了。我要求所有的组合,每个单词只出现一次。。。但是谢谢你的想法“每个词只出现一次”我错过了这个要求。在本例中,您向循环“for(int composition…”添加了一个过滤器这防止了错误的输入。谢谢你的回答,但是这里数组中的字符串数量现在已经提前知道了。我的意思是,它是一个参数,在不同的运行中会发生变化。因此,我不能像你的代码中那样,对
循环使用硬编码的
。当然,你可以动态地填充输入参数。没有必要把它们硬编码。这只是一个简单的例子,重点放在算法上,而不是输入和输出上。你读入你的输入文件,然后把它传递给getAll方法。到那时你应该知道长度了。我要求所有的组合,每个词只出现一次……但谢谢你的思考方式“其中每个单词只出现一次”我错过了该要求。在本例中,您为循环添加了一个过滤器“for(int composition…”,以防止输入错误。