面试题 08.08
Write a method to compute all permutations of a string whose charac ters are not necessarily unique. The list of permutations should not have duplicates.
Example1:
Input: S = "qqe" Output: ["eqq","qeq","qqe"] Example2:
Input: S = "ab" Output: ["ab", "ba"] Note:
All characters are English letters. 1 <= S.length <= 9
Solutions
dfs with swap and hashmap
Construct the permutation inplace and avoid visiting the same character in each searching step by using a hash map.
dfs with hash map
Avoid duplicates by fetching characters from hash map.
Last updated
Was this helpful?