[英]Generate permutations of strings with lengths 2 to N given N letters in Java
Given the following recursive Java method: 给定以下递归Java方法:
protected static void makeStringsList(List<String> strings, List<Character> letters) {
if (letters.size() == 2) {
strings.add(letters.get(0) + "" + letters.get(1));
strings.add(letters.get(1) + "" + letters.get(0));
}
else {
Character c = letters.remove(0);
makeStringsList(strings, letters);
List<String> tempList = new ArrayList<String>();
for (String s : strings) {
StringBuffer buffer = new StringBuffer(s);
for (int index = 0; index < s.length() + 1; index++) {
buffer.insert(index, c);
tempList.add(buffer.toString());
buffer = new StringBuffer(s);
}
}
strings.addAll(tempList);
}
}
Given N letters, the code above generates a list that contains permutations of strings that use all of those same N letters. 给定N个字母,上面的代码生成一个列表,其中包含使用所有相同N个字母的字符串的排列。 For example, given (1, 2, 3), it generates: 例如,给定(1,2,3),它将生成:
I had originally created the method above for a kata I was previously working on here during my free time, and I want to modify it now so that I can make it more general purpose and return a list containing permutations of strings with lengths 2 to N given N letters. 我最初为上面的kata创建了上面的方法,之前我在空闲时间在这里工作过,现在我想对其进行修改,以便使其更通用,并返回包含长度为2到N的字符串排列的列表。给N个字母。 Is it possible to modify the method above to accomplish this task? 是否可以修改上面的方法来完成此任务? Any tips? 有小费吗? Your advice will be greatly appreciated! 您的建议将不胜感激!
I would like to help out with this problem. 我想解决这个问题。
I think the best way to go about this is to generate the power set of the set of characters given, and then find the set of combinations of strings that each set in the power set can generate. 我认为解决此问题的最佳方法是生成给定字符集的幂集,然后找到幂集中每个集可以生成的字符串组合集。
List <String> allPermutationsOfSubsequences( Set <Character> chars ) {
Set < Set <Character> > powerSetOfChars = generatePowerSet ( chars );
List <String> permutations = new ArrayList <String> ();
for (Set <Character> subsequence : powerSetOfChars)
permutations.addAll( generatePermutations ( subsequence ) );
return permutations;
}
Set <Set <Character>> generatePowerSet ( Set <Character> set ) {
Set < Set <Character> > powerSet = new HashSet < Set <Character> > ();
if (set.size() == 0) {
powerSet.add(new HashSet <Character> ());
return powerSet;
}
Character anElement = set.iterator().next();
set.remove(anElement);
for (Set <Character> subset : powerSet(set)) {
Set <Character> setWithElement = new HashSet <Character> ();
setWithElement.add(anElement);
setWithElement.addAll(subset);
powerSet.add(newSet);
powerSet.add(subset);
}
set.add(anElement);
return powerSets;
}
//Generates a list of permutations of the characters provided in the set.
List <String> generatePermutations ( Set <Character> chars );
The generatePowerSet
method creates all the sets, so it also includes sets of size 0 and 1. You can remove those if you like, but the main idea is there. generatePowerSet
方法将创建所有集合,因此它还包括大小为0和1的集合。您可以根据需要删除它们,但是主要思想在那里。
Sample output: [3, 2, 1, 31, 13, 32, 23, 21, 12, 321, 312, 231, 213, 132, 123] 样本输出:[3、2、1、31、13、32、23、21、12、321、312、231、213、132、123]
All you need to do is remove the ones of size 1. 您需要做的就是删除尺寸为1的尺寸。
For the full code, that has been compiled and has shown to work, just go here and try it yourself! 要获取完整的代码,该代码已编译并可以正常工作,请直接在这里尝试一下!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.