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. hrb 2134 素数

    分拆素数和 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 176(99 users) Total Accepted: 106(93 u ...

  2. BZOJ 1002 [ FJOI 2007 ]

    -------------------------萌萌哒分割线------------------------- 题目很容易看懂,数据范围也不大.当然可以卡过暴力的人了. 在n=1时很明显是一种,如下 ...

  3. snoopy 强大的PHP采集类使用实例代码

    下载地址: http://www.jb51.net/codes/33397.html Snoopy的一些特点: 1抓取网页的内容 fetch 2 抓取网页的文本内容 (去除HTML标签) fetcht ...

  4. 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验

      12.数据类型转换.格式化.校验 12.1.数据绑定流程     Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFacto ...

  5. html css js

    html 回顾 字体:font 属性: color: 颜色 size: 字号 表格:table 标签: tr:表格中的行 td: 单元行中的单元格 th:通常使用在table中的第一行, 成为表头, ...

  6. iOS 端的第三方语音识别库

    最近在看语音识别方面的库,主要看了2个收费的项目,一个是 At&t 的,一个是Nuance的.这2个项目虽然是收费的,但是仅仅测试的话,是免费的,连接如下 https://developer. ...

  7. 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. ACdream 1195 Sudoku Checker (数独)

    Sudoku Checker Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  9. NEFU 2016省赛演练一 I题 (模拟题)

    这题没名字 Problem:I Time Limit:2000ms Memory Limit:65535K Description Now give you an interger m and a s ...

  10. ubuntu 彻底删除卸载

    1911 sudo apt-get install zabbix-agent 1916 sudo apt-get autoremove zabbix_agent root@(none):~# apt- ...