生成Java中字符串列表的单词组合

huangapple go评论86阅读模式
英文:

Generate word combinations from a list of strings in java

问题

I'm sorry, but it seems like you're asking for assistance with a programming problem, and you've requested not to provide a translation of the code. However, I can offer a brief summary of the issue you're facing in English.

You want to create a Java algorithm that takes a list of strings and returns all possible combinations of those strings. The issue with your current code is that it returns combinations in a different format than what you desire. You want a list of strings that includes individual elements and combinations, while your code is returning an array of arrays.

To achieve the desired result, you need to modify your code to accumulate both individual elements and combinations into a single list of strings. You can achieve this by iterating through the list of strings and creating combinations using nested loops.

If you have any specific questions or need further assistance with your code, please feel free to ask.

英文:

I wanted to create an algorithm in java where I can pass a list of strings and it returns a new list with all possible combinations of the strings it has in the list.

Example:

String[] listWords = new String[] {

                "windows",
                "linux",
                "mac",
                "10",
                "20"

        };

I would like to call a method by passing the list that returns all possible combinations, in all orders.

combinations(listWords);

This is the result I wanted to have:

windows,linux,mac,10,20,windowslinux,linuxwindows,windowsmac,windows10,10windows,windows20,20windows,windowslinuxmac1020,windowsmaclinux20,mac10,mac20,20mac,20mac10,windowsmac,macwindows...

I tried this:

public String[][] combinations (String[] ports) {
        List<String[]> combinationList = new ArrayList<String[]>();
        for ( long i = 1; i < Math.pow(2, ports.length); i++ ) {
            List<String> portList = new ArrayList<String>();
            for ( int j = 0; j < ports.length; j++ ) {
                if ( (i & (long) Math.pow(2, j)) > 0 ) {
                    portList.add(ports[j]);
                }
            }
            combinationList.add(portList.toArray(new String[0]));
        }
        return combinationList.toArray(new String[0][0]);
    }

But this returns:

生成Java中字符串列表的单词组合

This was not how I wanted it. The result had to be:

list: [windows, linux, windowslinux, linuxwindows, windows10, 10windowsmaclinux...]

Is it possible to do this in java? thank you who can help 生成Java中字符串列表的单词组合

答案1

得分: 1

如果我理解你的问题正确,以下方法可以解决这个问题。

你可以从你已经有的单词列表开始,迭代地构建结果,每次迭代都会添加越来越长的单词。第一次迭代给你原始的列表。第二次迭代给每个单词添加一个新单词,从而得到2个单词的排列组合。第三次迭代给每个已有排列组合添加一个新单词,从而得到3个单词的排列组合,依此类推。

List<String> getWordPermutations(List<String> words) {

    List<String> result = new ArrayList<>(words);
    List<String> oldPermutations = new ArrayList<>(words);

    for (int i = 1; i < words.size(); i++) {
        List<String> newPermutations = new ArrayList<>();
        for (String previousList : oldPermutations) {
            for (String word : words) {
                if (previousList.contains(word)) {
                    continue;
                }
                newPermutations.add(previousList + word);
            }
        }
        oldPermutations = newPermutations;
        result.addAll(newPermutations);
    }

    return result;
}

希望这对你有所帮助。

英文:

If I've understood you correctly, the following will solve the problem.

You can build the result iteratively starting from the list of words that you have, and in each iteration you add longer and longer words. The first iteration gives you the original list. The second iteration adds one new word to each, giving you the permutations of 2 words. The third iteration adds one new word to each of those, giving you the permutations of 3 words, and so on.

List&lt;String&gt; getWordPermutations(List&lt;String&gt; words) {

    List&lt;String&gt; result = new ArrayList&lt;&gt;(words);
    List&lt;String&gt; oldPermutations = new ArrayList&lt;&gt;(words);

    for (int i = 1; i &lt; words.size(); i++) {
        List&lt;String&gt; newPermutations = new ArrayList&lt;&gt;();
        for (String previousList : oldPermutations) {
            for (String word : words) {
                if (previousList.contains(word)) {
                    continue;
                }
                newPermutations.add(previousList + word);
            }
        }
        oldPermutations = newPermutations;
        result.addAll(newPermutations);
    }

    return result;
}

huangapple
  • 本文由 发表于 2020年8月5日 08:50:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63256902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定