英文:
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);//第一次迭代我们得到 [ż, ź, ž] 第二次迭代我们得到 [à, á, â, ã, ä, å, ą, ă, ā]
mainList.add(subList);//[[ż, ź, ž], [à, á, â, ã, ä, å, ą, ă, ā]]
}
}
现在我想生成来自[[ż, ź, ž], [à, á, â, ã, ä, å, ą, ă, ā]]
的String
组合。String
可以是azstu等等。我如何迭代,以便它会考虑输入String
的所有关联组合。请给予建议。
从示例字符串中的期望输出:
żà
żá
żâ
żã
żä
żå
żą
żă
żā
.
.
.
希望这能帮助您解决问题。
英文:
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="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);//first iteration we get [ż, ź, ž] second iteration we get [à, á, â, ã, ä, å, ą, ă, ā]
mainList.add(subList);//[[ż, ź, ž], [à, á, â, ã, ä, å, ą, ă, ā]]
}
}
Now i want to generate combination of String
from [[ż, ź, ž], [à, á, â, ã, ä, å, ą, ă, ā]]
.
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
得分: 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<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;
}
And call like
List<List<String>> mainList = new ArrayList<List<String>>();
mainList.add(Arrays.asList("ż", "ź", "ž"));
mainList.add(Arrays.asList("à", "á", "â", "ã", "ä", "å", "ą", "ă", "ā"));
List<String> data = genr(mainList, 0, "");
Output:
[żà, żá, żâ, żã, żä, żå, żą, żă, żā, źà, źá, źâ, źã, źä, źå, źą, źă, źā, žà, žá, žâ, žã, žä, žå, žą, žă, žā]
Demo here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论