面试题38
输入一个字符串,打印出该字符串中字符的所有排列。
你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。
限制:
1 <= s 的长度 <= 8
Solutions
backtrack with counter
backtrack with swap
In each recursive call, using numbers(characters) with the same value are forbidden.
Since swap could break the continuousness of two numbers, additional steps are required for checking duplicates.
Or another version based on sorted string, we always choose to swap the current character with the inserted character(first) and do not swap back.
For example:
aabbcc
, when the insertion position is0
, there are3
possible strings.aabbcc
baabcc
caabbc
The ordering of each pair of character doesn't change after the swap operation which is different from that in the previous solution.
Last updated
Was this helpful?