ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 字符数组的每个组合

2019-10-01 09:03:46  阅读:188  来源: 互联网

标签:anagram java string permutation


在尝试显示数组字符的每个组合而不重复字母时遇到问题.

public static String[] getAllLists(String[] elements, int lengthOfList)
{
    //initialize our returned list with the number of elements calculated above
    String[] allLists = new String[(int)Math.pow(elements.length, lengthOfList)];

    //lists of length 1 are just the original elements
    if(lengthOfList == 1) return elements; 
    else
    {
        //the recursion--get all lists of length 3, length 2, all the way up to 1
        String[] allSublists = getAllLists(elements, lengthOfList - 1);

        //append the sublists to each element
        int arrayIndex = 0;

        for(int i = 0; i < elements.length; i++)
        {
            for(int j = 0; j < allSublists.length; j++)
            {
                //add the newly appended combination to the list
                allLists[arrayIndex] = elements[i] + allSublists[j];
                arrayIndex++;
            }
        }

        return allLists;
    }
}

上面的代码很完美,但在这种情况下不止一次地使用每个字母.

我现在被困在如何做到这一点.

解决方法:

这是一个示例实现.本质上,它需要一个字符串并迭代每个字符,将该字符放在前面.然后它会对其余字符进行递归.该结构消除了重复字母的问题,因为递归的输入已删除了您已使用过的字符.

我还将结果存储在一个集合中以删除语义等价.输入’aab’可以切换char 0和char 1,但仍然是’aab’.我使用TreeSet来保存排序以便更容易地验证输出,但HashSet会更快.

  public static Set<String> permute(String chars)
  {
    // Use sets to eliminate semantic duplicates (aab is still aab even if you switch the two 'a's)
    // Switch to HashSet for better performance
    Set<String> set = new TreeSet<String>();

    // Termination condition: only 1 permutation for a string of length 1
    if (chars.length() == 1)
    {
      set.add(chars);
    }
    else
    {
      // Give each character a chance to be the first in the permuted string
      for (int i=0; i<chars.length(); i++)
      {
        // Remove the character at index i from the string
        String pre = chars.substring(0, i);
        String post = chars.substring(i+1);
        String remaining = pre+post;

        // Recurse to find all the permutations of the remaining chars
        for (String permutation : permute(remaining))
        {
          // Concatenate the first character with the permutations of the remaining chars
          set.add(chars.charAt(i) + permutation);
        }
      }
    }
    return set;
  }

示例运行:

  public static void main(String[] args)
  {
    for (String s : CharPermuter.permute("abca"))
    {
      System.out.println(s);
    }
  }

产生:

aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa

标签:anagram,java,string,permutation
来源: https://codeday.me/bug/20191001/1838118.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有