【问题标题】:Recursive String permutation with repetitions allowed允许重复的递归字符串排列
【发布时间】:2014-12-03 20:42:07
【问题描述】:

您好,我正在尝试找出一种有效的方法来获取字符串的所有排列,但也允许重复字符。 (例如对于字符串“ab”,输出将是“aa, bb, ba, ab”)

我的代码在没有重复的情况下正常工作,但我不确定如何修改它以允许重复字符,并且似乎只能找到在不重复字符的情况下执行此操作的方法。

这是我的代码:

    public static ArrayList<String> permutationsUnique(String word) {
    ArrayList<String> result = new ArrayList<String>();

    if (word.length() == 0) {
        result.add(word);
        return result;
    } else {

        for (int i = 0; i < word.length(); i++) {

            String shorter = word.substring(0, i) + word.substring(i + 1);

            ArrayList<String> shorterPermutations = permutationsUnique(shorter);

            for (String s : shorterPermutations) {
                result.add(word.charAt(i) + s);
            }
        }

        return result;
    }

}

如果有人能指引我正确的方向,我将不胜感激。

谢谢

【问题讨论】:

  • 你需要用递归来实现吗?每个排列的长度是否有限制?例如,"bba""aba""abb" 也是仅由字母 'a''b' 创建的排列。
  • in string "cat" output "tta" 将是一个有效的输出。
  • 在这种情况下“ttat”是一个有效的输出吗?我的示例在原始案例中是有效的输出吗?
  • 一个有效的输出不应超过原始字符串的 o 个字符,但它可以忽略某些字符。如果原始单词是“cats”,“ttat”将是一个好的输出

标签: java string recursion permutation


【解决方案1】:

抱歉,我之前的回答有些无关紧要。已删除。这肯定会奏效:-

static void Recur(String prefix, String str)
{
 if(prefix.length()==str.length())
 { 
   System.out.println(prefix); return; 
 }

   for(int i=0; i<str.length(); i++)
   Recur(prefix+str.charAt(i),str);
 }
 public static void main (String[] args)
 {
    String str = "AB";
 }

输出是

AA
AB
BA
BB

【讨论】:

  • 什么是前缀?我相信我需要将方法的标题保留为: public static ArrayList permutationsUnique(String word) {
  • prefix 基本上是将使用递归添加到前面的字符串。您必须通过代码才能理解它的含义,因为它太复杂而无法解释递归过程。
  • 我的方法和你的不同。您使用了arraylist,但我使用了推荐的字符串。
  • 我明白了...递归方法应该是返回字符串的辅助方法。我是这样称呼它的:for (String s : permutations(keyWord))....
【解决方案2】:

我知道很久以前就有人问过这个问题,但我只是在一个 java 程序上工作,以允许重复的整数排列。我把我的代码放在这里,以防有人需要。

这个程序可以生成指定长度的排列。 IE。 对于整数 1,2 和 lengthOfSinglePermutation = 2,输出应该是 11 21 12 22

对于整数 1,2 和 lengthOfSinglePermutation = 3,输出应该是 111 211 121 221 112 212 122 222

希望这会有所帮助。

    public class PermutationsWithRepetitions {

public static void main(String[] args) {
    int[] input = { 1, 2 };
    int lengthOfSinglePermutation = 3;

    // we need to check number of unique values in array
    Set<Integer> arrValues = new HashSet<Integer>();
    for (int i : input) {
        arrValues.add(i);
    }
    int noOfUniqueValues = arrValues.size();

    int[] indexes = new int[lengthOfSinglePermutation];
    int totalPermutations = (int) Math.pow(noOfUniqueValues, lengthOfSinglePermutation);

    for (int i = 0; i < totalPermutations; i++) {

        for (int j = 0; j < lengthOfSinglePermutation; j++) {
            System.out.print(input[indexes[j]]);
        }
        System.out.println();

        for (int j = 0; j < lengthOfSinglePermutation; j++) {
            if (indexes[j] >= noOfUniqueValues - 1) {
                indexes[j] = 0;
            }
            else {
                indexes[j]++;
                break;
            }
        }
    }
}
}

【讨论】:

    【解决方案3】:

    我有另一个使用 char 数组的解决方案,但它适用于任何类型的数组。在这里,您可以添加列表并将元素附加到其中,而不是打印字符(或您想要的任何数据类型)。

    static void permuteAll(char [] a)
    {   
        permuteArray(a, new char[a.length], 0);
    }
    
    static void permuteArray(char [] a, char [] s, int j)
    {
        if(j == a.length)
        {
            System.out.println(new String(s));
            return;
        }
    
        for(int i = 0; i < a.length; i++)
        {
            s[j] = a[i];
            permuteArray(a, s, j+1);
        }
    
    }
    

    【讨论】:

      【解决方案4】:
      public static void main(String args[]){
          String str="ABC";
          int maxSize =3;
          char[] chars = str.toCharArray();
          getCombination(chars, maxSize); 
      }
      static void getCombination(char[]chars, int maxSize){
         
        for(int i=0; i<chars.length; i++){
         combination(String.valueOf(chars[i]),chars,maxSize);
        }
      }
      
      static void combination(String prefix,char[] chars, int maxSize) {
      if(prefix.length()>=maxSize){
          System.out.println(prefix);
      }
      else {
         for(int j= 0; j<chars.length;j++)
         {
            String newPrefix =prefix.concat(String.valueOf(chars[j]));
            combination(newPrefix,chars, maxSize);
         
        }
      }
      }
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2011-12-23
      • 2012-08-24
      相关资源
      最近更新 更多