String Permutation

Given a string, find all permutations of it without duplicates.

Example

Given "abb", return ["abb", "bab", "bba"].

Given "aabb", return ["aabb", "abab", "baba", "bbaa", "abba", "baab"].

思路:permutation 标准的解题步骤:递归 + 回溯。 StringBuilder 有一个删除某一位置字符的函数deleteCharAt(index).

 1 public class Solution {
 2     /**
 3      * @param str a string
 4      * @return all permutations
 5      */
 6     public List<String> stringPermutation2(String str) {
 7        List<String> result = new ArrayList<>();
 8
 9         char[] ch = str.toCharArray();
10         Arrays.sort(ch);
11         boolean[] visited = new boolean[ch.length];
12         StringBuilder builder = new StringBuilder();
13         helper(result, builder, ch, visited);
14         return result;
15     }
16
17     private void helper(List<String> result, StringBuilder builder, char[] ch, boolean[] visited) {
18         if (builder.length() == ch.length) {
19             String s = builder.toString();
20             result.add(s);
21             return;
22         }
23
24         for (int i = 0; i < ch.length; i++) {
25             if (visited[i] || i != 0 && ch[i] == ch[i - 1] && visited[i - 1] == false) {
26                 continue;
27             }
28
29             visited[i] = true;
30             builder.append(ch[i]);
31             helper(result, builder, ch, visited);
32             builder.deleteCharAt(builder.length() - 1);
33             visited[i] = false;
34         }
35     }
36 }
时间: 2024-07-28 16:57:03

String Permutation的相关文章

28. 字符串的全排列之第2篇[string permutation with repeating chars]

[本文链接] http://www.cnblogs.com/hellogiser/p/string-permutation-with-repeating-chars.html [题目] 输入一个字符串,打印出该字符串中字符的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab和cba.例如输入字符串aba,则输出由字符a.b所能排列出来的所有字符串aab.aba.baa. [分析] 之前的博文28.字符串的排列之第1篇[String

Palindrome Permutation I &amp; II

Palindrome Permutation I Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even

Permutation Sequence LT60

The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for n = 3: "123" "132" "213" "231" "312" "321&

剑指offer(26-30)编程题

二叉搜索树与双向链表 字符串的排列 数组中出现次数超过一半的数字 最小的K个数 连续子数组的最大和 26.输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向 class Solution { public: //0:left 1: right TreeNode* doConvert(TreeNode* pRootOfTree, int leftOrRight) { if (pRootOfTree == nullptr) return

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se

输出字符串的所有排列

题目:终端随机输入一串字符串,输出该字符串的所有排列. 例如,输入:"abc",输出:abc.acb.bac.bca.cab.cba 递归解决: 关键在于递归条件.边界条件(start==end).去重(例如"aa",可以借助HashSet进行去重) 关键词:permutation [p??mj?'te??(?)n] 排列,置换 recursion [r?'k???(?)n]  递归,循环 具体代码参看如下: 1 import java.util.ArrayList

剑指offer 66题 -- 字符串的排列

class Solution {public: vector<string> Permutation(string str) { vector<string> result; if(str.size()<=0) return result; int start = 0; permute( str, start, result); sort( result.begin(), result.end() ); return result; } //采用传引用的方式,处理递归过程中的

字符串全排列和组合算法

打印字符串的全排列 算法的思路: 把一个字符串分成两部分,第一个字符+后面部分所有的字符.这样就能够递归的求解整个过程了: 1.每个字符都做一次首字符 2.当某个字符作为首字符的时候,求后面所有字符的全排列 而这里的求后面所有字符的全排列可以看成递归的子问题 全排列的递归树: 但是这里还有一个问题,那就是字符串中有重复的字符时,这样的算法会增加最后的结果数目.比如说字符串aab,a+ab的全排列,然后交换还是a+ab的全排列.所以会增加结果的数目.解决方案就是:当遇到重复的字符的时候就跳过去,不

[笔试题]字符串的排列和组合

[代码] C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411