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

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

Every combination of characters from dynamic list

问题

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

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

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

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

  1. ż&#224;
  2. ż&#225;
  3. ż&#226;
  4. ż&#227;
  5. ż&#228;
  6. ż&#229;
  7. żą
  8. żă
  9. żā
  10. .
  11. .
  12. .

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

英文:

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.

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

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:-

  1. ż&#224;
  2. ż&#225;
  3. ż&#226;
  4. ż&#227;
  5. ż&#228;
  6. ż&#229;
  7. żą
  8. żă
  9. żā
  10. .
  11. .
  12. .

答案1

得分: 0

以下是翻译后的内容:

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

  1. List<String> genr(List<List<String>> list, int index, String now) {
  2. if (index >= list.size()) {
  3. return Arrays.asList(now);
  4. }
  5. List<String> subList = list.get(index);
  6. List<String> res = new ArrayList<>();
  7. for (String value : subList) {
  8. res.addAll(genr(list, index + 1, now + value));
  9. }
  10. return res;
  11. }

调用方式如下:

  1. List<List<String>> mainList = new ArrayList<List<String>>();
  2. mainList.add(Arrays.asList("ż", "ź", "ž"));
  3. mainList.add(Arrays.asList("à", "á", "â", "ã", "ä", "å", "ą", "ă", "ā"));
  4. List<String> data = genr(mainList, 0, "");
  5. 输出
  6. `[żà, żá, żâ, żã, żä, żå, żą, żă, żā, źà, źá, źâ, źã, źä, źå, źą, źă, źā, žà, žá, žâ, žã, žä, žå, žą, žă, žā]`

演示见此处

英文:

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

  1. List&lt;String&gt; genr(List&lt;List&lt;String&gt;&gt; list, int index, String now) {
  2. if (index &gt;= list.size()) {
  3. return Arrays.asList(now);
  4. }
  5. List&lt;String&gt; subList = list.get(index);
  6. List&lt;String&gt; res = new ArrayList&lt;&gt;();
  7. for (String value : subList) {
  8. res.addAll(genr(list, index + 1, now + value));
  9. }
  10. return res;
  11. }

And call like

  1. List&lt;List&lt;String&gt;&gt; mainList = new ArrayList&lt;List&lt;String&gt;&gt;();
  2. mainList.add(Arrays.asList(&quot;ż&quot;, &quot;ź&quot;, &quot;ž&quot;));
  3. 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;));
  4. 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:

确定