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. PHP+七牛云存储上传图片代码片段

    2014年11月14日 16:37:51 第一段代码是上传单个图片的,第二个是上传多个图片的 //上传到七牛 //单个文件 //formname: 表单名字; pre: 图片Url中显示的图片名字(也 ...

  2. php 字符串负值判断

    2014年9月9日 11:54:54 $a = '-1'; $b = (int)$a; $c = is_numeric($a); if ($a) { echo 1; //echo 1 } else { ...

  3. codeforces B. Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...

  4. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  5. 配置SecureCRT连接本地虚拟机中的Linux系统

    转自:http://www.pythoner.com/196.html 由于平时公司开发时都是使用SecureCRT连接的Linux服务器,所以也想使用SecureCRT在自己电脑上连接本地虚拟机中的 ...

  6. Fresco 源码分析(三) Fresco服务端处理(1) ImagePipeline为何物

    4.3 服务端的处理 备注: 因为是分析,而不是设计,所以很多知识我们类似于插叙的方式叙述,就是用到了哪个知识点,我们再提及相关的知识点,如果分析到了最后,我想想是不是应该将这个架构按照设计的方式,重 ...

  7. 7.适配器模式(Adapter Pattern)

    using System; namespace Test { /// <summary> /// 适配器模式主要解决的问题就是我们要调用的接口类型,无法满足我们新系统的使用需求, /// ...

  8. x264码率控制方法介绍

    转自:http://www.bubuko.com/infodetail-471698.html 1.  X264显式支持的一趟码率控制方法有:ABR, CQP, CRF. 缺省方法是CRF.这三种方式 ...

  9. 继承ActionSupport,返回INPUT的原因

    http://developer.51cto.com/art/200907/134757.htm 表面现象: 在WebWork中,当一个Action中既没有重写ActionSupport中的valid ...

  10. php 批量生成html、txt文件

    首先建立一个conn.php的文件用来链接数据库 <?php     $link = mysql_connect("mysql_host" , "mysql_use ...