简体   繁体   English

在Java中生成长度为2到N的字符串的排列(给定N个字母)

[英]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),它将生成:

  • 123 123
  • 132 132
  • 213 213
  • 231 231
  • 312 312
  • 321 321
It also adds strings with fewer than N letters to the list, but they're not exhaustive. 它还将少于N个字母的字符串添加到列表中,但是它们并不详尽。 Using the same example, 23 and 32 are also in the list, but 12, 21, 13, and 31 are not in the list. 使用相同的示例,列表中也有23和32,但是列表中没有12、21、13和31。

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! 要获取完整的代码,该代码已编译并可以正常工作,请直接在这里尝试一下!

http://pastebin.com/P3YMmT2m http://pastebin.com/P3YMmT2m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找条件为给定N的所有排列的算法 - Algorithm to find all permutations of a given N with condition 生成长度为 N 的数字的所有排列 - Generate all permutations of digits of length N 如何检查两个字符串在O(n)时间中是否彼此置换? (java) - How can I check if two strings are permutations of each other in O(n) time? (java) 如何从给定的日期生成N周数-Java - How to generate N number of weeks from the date given - java (Java) 返回当前字符串的新字符串版本,其中所有 >= 或 <= 给定字符 n 的字母都被删除 - (Java) Returns a new string version of the current string where all the letters either >= or <= the given char n, are removed 创建 n 大小的排列 - Creating permutations of n size 使用递归在Java中打印长度为N的位串的排列 - Using recursion to print permutations of a bit string of length N in Java 两个字符串的字符串匹配的前n个字母 - String-matching first n letters of two strings 如何检查2个字符串是否具有n个字母的公共子字符串 - How to check if 2 strings have common substrings with n letters 给定数字N和N个字符串数组,请基于每个字符串中的元音数量以降序对字符串进行排序 - Given a number N and an array of N strings, sort the strings based on number of vowels in each of the strings in the descending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM