字符串全排列(permutation)
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)的更多相关文章
- 字符串全排列 java实现
经常会遇到字符串全排列的问题.例如:输入为{‘a’,’b’,’c’},则其全排列组合为abc,acb,bac,bca,cba,cab.对于输入长度为n的字符串数组,全排列组合为n!种. package ...
- 【Data Structure & Algorithm】字符串全排列
字符串全排列 题目:输入一个字符串,打印出该字符串的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab.cba. 分析:考察对递归的理 ...
- PHP字符串全排列算法
<?php /** * PHP字符串全排列算法 */ $results = []; $arr = []; function bfs($start) { global $arr; global $ ...
- 基于visual Studio2013解决面试题之0708字符串全排列
题目
- 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation
[抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...
- 笔试算法题(16):二叉树深度计算 & 字符串全排列
出题:要求判断二元树的深度(最长根节点到叶节点的路径): 分析:二元递归不容易使用循环实现 解题: struct Node { int value; Node *left; Node *right; ...
- nodejs 字符串全排列 和 去重
以前写了个java版的 现在写个nodejs 版的 var list = sort('CCAV');var noRepeat = {};for(var i in list){ noRepeat[lis ...
- java 字符串全排列 和 去重
用递归进行排序 , 用TreeSet 去重. public class test { public static void main(String []args){ String str = &quo ...
- 全排列 permutation
给定一个数字列表,返回其所有可能的排列 lintcode package www.dxb.com; import java.util.List;import java.util.ArrayList; ...
随机推荐
- linux 如何清理僵尸进程
今天在维护服务器的时候,发现有5个nova-novncproxy的僵尸进程. 26327 ? S 0:05 \_ /usr/bin/python /usr/bin/nova- ...
- 基本二叉搜索树的第K小元素
#include<stdio.h> #include<stdlib.h> typedef struct node *btlink; struct node { int data ...
- wget批量下载
wget -i download.txt 这样就会把download.txt里面列出的每个URL都下载下来. wget -c http://the.url.of/incomplete/file 使用断 ...
- Rehashing
The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...
- Android 和iOS 创建本地通知
1 Android 中的发送本地通知的逻辑如下 先实例化Notification.Builder,再用builder创建出具体的Notification,创建时要指定好启动用的PendingInten ...
- Android 中PendingIntent---附带解决AlarmManager重复加入问题
最近在程序中使用到了notification功能,自然,就涉及到了PendingIntent,下面总结下. 1 什么是PendingIntent A description of an Intent ...
- 低配置电脑播放 flash 视频时 占 cpu 资源过高的解决方法
安装低版本的 flash player 版本, 经调试能满足播放的最低版本是 Flash Player 10.3.183.90 然后 firefox 3.6.28 + Adblock Plus 2.0 ...
- HDU 1003 Max Sum
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- codeforces B. Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...
- CSS鼠标样式整理
鼠标样式的标签: cursor:*; //该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状: 鼠标样式: 值 描述 url 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一 ...