每个动态列表中的字符组合

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

Every combination of characters from dynamic list

问题

我被困在一个逻辑问题中。我有一个自由文本下拉框,用户可以自由输入最多5个字符。现在每个字符都有与之关联的Unicode重音字符。我为字符关联的列表生成Unicode字符。现在我想生成所有可能的组合,使用来自列表的字符。由于列表是动态生成的,我有点卡住了。我如何可能知道应该首先迭代哪个列表?下面是我的代码。

// 例子:String key = "za";
if (key != null) {
    List<Character> characList = key.chars()
                                    .mapToObj(c -> (char) c)
                                    .collect(Collectors.toList());//[z, a]
    List<List<String>> mainList = new ArrayList<List<String>>();
    for (Character characterObj : characList) {
        List<String> subList = new ArrayList<String>();
        Collection<String> charColl = unicodeMap.getCollection(characterObj.toString());
        subList = new ArrayList(charColl);//第一次迭代我们得到 [ż, ź, ž] 第二次迭代我们得到 [&#224;, &#225;, &#226;, &#227;, &#228;, &#229;, ą, ă, ā]
        mainList.add(subList);//[[ż, ź, ž], [&#224;, &#225;, &#226;, &#227;, &#228;, &#229;, ą, ă, ā]]
    }
}

现在我想生成来自[[ż, ź, ž], [&#224;, &#225;, &#226;, &#227;, &#228;, &#229;, ą, ă, ā]]String组合。String可以是azstu等等。我如何迭代,以便它会考虑输入String的所有关联组合。请给予建议。

从示例字符串中的期望输出:

ż&#224;
ż&#225;
ż&#226;
ż&#227;
ż&#228;
ż&#229;
żą
żă
żā
.
.
.

希望这能帮助您解决问题。

英文:

I am stuck with a logic. I have a free text dropdown where users are free to enter up to 5 characters. Now every character has unicode accent characters associated to it. I generate unicode characters for the list associated for the characters. Now I want to generate all possible combinations with the characters from the list. I am a bit stuck since the list is generated dynamically. How do I possibly get to know which list should be iterated first? Below is my code.

//example String key=&quot;za&quot;;
if (key!=null) {
	List&lt;Character&gt; characList = key.chars()
                                    .mapToObj(c -&gt; (char) c)
                                    .collect(Collectors.toList());//[z, a]
	List&lt;List&lt;String&gt;&gt; mainList = new ArrayList&lt;List&lt;String&gt;&gt;();
	for (Character characterObj:characList) {
		List&lt;String&gt; subList = new ArrayList&lt;String&gt;();
		Collection&lt;String&gt; charColl = unicodeMap.getCollection(characterObj.toString());
		subList = new ArrayList(charColl);//first iteration we get [ż, ź, ž] second iteration we get [&#224;, &#225;, &#226;, &#227;, &#228;, &#229;, ą, ă, ā]
		mainList.add(subList);//[[ż, ź, ž], [&#224;, &#225;, &#226;, &#227;, &#228;, &#229;, ą, ă, ā]]
	}
}

Now i want to generate combination of String from [[ż, ź, ž], [&#224;, &#225;, &#226;, &#227;, &#228;, &#229;, ą, ă, ā]].
The String could have been azstu,etc. How do i iterate so that it will take care of all combinations from the associations of entered String. Please advise.

Desired output from the example String:-

ż&#224;
ż&#225;
ż&#226;
ż&#227;
ż&#228;
ż&#229;
żą
żă
żā
.
.
.

答案1

得分: 0

以下是翻译后的内容:

一种方法是通过递归生成字符串。通过索引遍历字符的子集合列表,并循环处理每个子列表。

List<String> genr(List<List<String>> list, int index, String now) {
    if (index >= list.size()) {
        return Arrays.asList(now);
    }
    List<String> subList = list.get(index);
    List<String> res = new ArrayList<>();
    for (String value : subList) {
        res.addAll(genr(list, index + 1, now + value));
    }
    return res;
}

调用方式如下:

List<List<String>> mainList = new ArrayList<List<String>>();
mainList.add(Arrays.asList("ż", "ź", "ž"));
mainList.add(Arrays.asList("à", "á", "â", "ã", "ä", "å", "ą", "ă", "ā"));
List<String> data = genr(mainList, 0, "");

输出
`[żà, żá, żâ, żã, żä, żå, żą, żă, żā, źà, źá, źâ, źã, źä, źå, źą, źă, źā, žà, žá, žâ, žã, žä, žå, žą, žă, žā]`

演示见此处

英文:

A way can be recursively generate the string. Traverse the character's collection list by index and looping for every sublist.

List&lt;String&gt; genr(List&lt;List&lt;String&gt;&gt; list, int index, String now) {
    if (index &gt;= list.size()) {
      return Arrays.asList(now);
    }
    List&lt;String&gt; subList = list.get(index);
    List&lt;String&gt; res = new ArrayList&lt;&gt;();
    for (String value : subList) {
      res.addAll(genr(list, index + 1, now + value));
    }
    return res;
  }

And call like

List&lt;List&lt;String&gt;&gt; mainList = new ArrayList&lt;List&lt;String&gt;&gt;();
mainList.add(Arrays.asList(&quot;ż&quot;, &quot;ź&quot;, &quot;ž&quot;));
mainList.add(Arrays.asList(&quot;&#224;&quot;, &quot;&#225;&quot;, &quot;&#226;&quot;, &quot;&#227;&quot;, &quot;&#228;&quot;, &quot;&#229;&quot;, &quot;ą&quot;, &quot;ă&quot;, &quot;ā&quot;));
List&lt;String&gt; data = genr(mainList, 0, &quot;&quot;);

Output:
[ż&#224;, ż&#225;, ż&#226;, ż&#227;, ż&#228;, ż&#229;, żą, żă, żā, ź&#224;, ź&#225;, ź&#226;, ź&#227;, ź&#228;, ź&#229;, źą, źă, źā, ž&#224;, ž&#225;, ž&#226;, ž&#227;, ž&#228;, ž&#229;, žą, žă, žā]

Demo here

huangapple
  • 本文由 发表于 2020年8月19日 13:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63480823.html
匿名

发表评论

匿名网友

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

确定