All Permutation II (with duplication and consider order)
PreviousAll Subset of K size III (with duplication without considering order)NextFactor Combination-质数分解
Last updated
Last updated
1. Link
2. 题目
Given a string with possible duplicate characters, return a list with all permutations of the characters.
Examples
Set = “abc”, all permutations are [“abc”, “acb”, “bac”, “bca”, “cab”, “cba”]
Set = "aba", all permutations are ["aab", "aba", "baa"]
Set = "", all permutations are [""]
Set = null, all permutations are []
3. 思路
先把重复的char用dictionary表示除重,key= char, value= count action = key
DFS method, 因为是考虑order顺序,所以需要用for loop对不同的char进行位置对换
对于重复的char,每次pick action时把它的count -= 1 表示剩下可以选的次数,如果没有,count=0,就选下一个action.这样能避免同一个level里面有相同的tree branch 分支,因为每次level选取的char都保证是不一样
recursion tree 变成: exmaple
4. Coding