Reference:

http://www.cnblogs.com/sujz/archive/2011/06/16/2082831.html

问题:给定字符串S,生成该字符串的全排列。

方法1:依次从字符串中取出一个字符作为最终排列的第一个字符,对剩余字符组成的字符串生成全排列,最终结果为取出的字符和剩余子串全排列的组合。

 public static void main(String[] args) {
Main so = new Main();
System.out.println("method 1:");
so.permutation("","ABA");
}
public void permutation(String prefix,String s){
if(s==null||s.length()==0)
System.out.println(prefix);
for(int i=0;i<s.length();i++){
permutation(prefix+s.charAt(i), s.substring(0, i)+s.substring(i+1,s.length()));
}
}

优点:该方法易于理解,但无法移除重复的排列,如:s="ABA",会生成两个“AAB”。

method 1:
ABA
AAB
BAA
BAA
AAB
ABA

方法2:利用交换的思想,具体见实例,但该方法不如方法1容易理解。

 package POJ;

 import java.util.HashMap;

 public class Main {

     /**
*
* 9.4 Write a method to compute all permutations of a string.
*
*
*/
public static void main(String[] args) {
Main so = new Main();
System.out.println("method 2:");
HashMap<String, Integer> hs=new HashMap<String, Integer>();
so.permutation2("AABB", 0, 3,hs);
} public void permutation2(String s, int startIndex, int endIndex,HashMap<String, Integer> hs) {
if (startIndex == endIndex) {
if(!hs.containsKey(s)){
hs.put(s, 1);
System.out.println(s);
}
}
for (int j = startIndex; j <= endIndex; j++) {
if ((s.charAt(j) == s.charAt(startIndex)) && (j != startIndex))
continue;
s = swap(s, startIndex, j);
permutation2(s, startIndex + 1, endIndex,hs);
s = swap(s, startIndex, j);
}
} public String swap(String s, int i, int j) {
char[] char_s = s.toCharArray(); char temp = char_s[i];
char_s[i] = char_s[j];
char_s[j] = temp; s = String.copyValueOf(char_s);
return s;
}
}

运行结果:

method 2:
AABB
ABAB
ABBA
BAAB
BABA
BBAA

原Reference里的那个哥们儿的代码有点问题,他的代码用java实现后只能完成类似于ABCD,AABC等string的无重复全排列,而无法做到AABB这种类型的无重复全排列。

字符串全排列(permutation)的更多相关文章

  1. 字符串全排列 java实现

    经常会遇到字符串全排列的问题.例如:输入为{‘a’,’b’,’c’},则其全排列组合为abc,acb,bac,bca,cba,cab.对于输入长度为n的字符串数组,全排列组合为n!种. package ...

  2. 【Data Structure & Algorithm】字符串全排列

    字符串全排列 题目:输入一个字符串,打印出该字符串的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab.cba. 分析:考察对递归的理 ...

  3. PHP字符串全排列算法

    <?php /** * PHP字符串全排列算法 */ $results = []; $arr = []; function bfs($start) { global $arr; global $ ...

  4. 基于visual Studio2013解决面试题之0708字符串全排列

     题目

  5. 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation

    [抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...

  6. 笔试算法题(16):二叉树深度计算 & 字符串全排列

    出题:要求判断二元树的深度(最长根节点到叶节点的路径): 分析:二元递归不容易使用循环实现 解题: struct Node { int value; Node *left; Node *right; ...

  7. nodejs 字符串全排列 和 去重

    以前写了个java版的 现在写个nodejs 版的 var list = sort('CCAV');var noRepeat = {};for(var i in list){ noRepeat[lis ...

  8. java 字符串全排列 和 去重

    用递归进行排序 , 用TreeSet 去重. public class test { public static void main(String []args){ String str = &quo ...

  9. 全排列 permutation

    给定一个数字列表,返回其所有可能的排列 lintcode package www.dxb.com; import java.util.List;import java.util.ArrayList; ...

随机推荐

  1. 2模02day1题解

    源文件在我的网盘上.链接:http://pan.baidu.com/s/1qWPUDRm 密码:k52e (只有机智的人才能看到我的链接) 机智的双重下划线~~~ T1 T1就是一个递推,这题目把我恶 ...

  2. set_include_path详细解释

    zendframework的示例index.php里有这样一句 set_include_path('.' . PATH_SEPARATOR . '../library/'. PATH_SEPARATO ...

  3. No bootable device-insert boot disk and press any key

    macbook air 2012 mid. 长按关机键关机,按开机键,然后长按option键,会出现可以选择启动的磁盘块,选择要启动的磁盘进入即可.

  4. django template中load的作用

    某些应用提供自定义标签和过滤器库. 要在一个模板中访问它们, 使用 {% load %} 标签: {% load comments %} {% comment_form for blogs.entri ...

  5. C# Dictionary和Dynamic类型

    开发中需要传递变参,考虑使用 dynamic 还是 Dictionary(准确地说是Dictionary<string,object>).dynamic 的编码体验显著优于 Diction ...

  6. Swap Two Nodes in Linked List

    Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 a ...

  7. WriteFile实现下载

    TransmitFile实现下载     protected void Button1_Click(object sender, EventArgs e)      {         /*      ...

  8. Linux下提取IP至文件

    ifconfig | grep 'inet[^6]' | sed 's/^\s*//g' | cut -d ' ' -f2 > ips.txt 排除127开头的IP: ifconfig | gr ...

  9. Java面向对象的封装

    封装是Java面向对象的三大特性之一,通常我们是通过包管理机制同时对类进行封装,隐藏其内部实现细节,通常开发中不允许直接操作类中的成员属性,所以属性一般设置为私有权限private,类中一般会给出一些 ...

  10. JS 中的foreach和For in比较

    使用方式举例如下: <script type="text/javascript"> var jsonranklist=[{"name":" ...