Redian新闻
>
String permunation question (CS)
avatar
String permunation question (CS)# JobHunting - 待字闺中
l*r
1
Old question, but can't find a good and easy to understand answer.
What happens if there are duplicate letters in the string? like, abcdd.
avatar
p*2
2
You shouldn't output duplicate permutations.
avatar
r*c
3
use recursive, fix the first letter then do permutation for the rest n-1.

【在 l********r 的大作中提到】
: Old question, but can't find a good and easy to understand answer.
: What happens if there are duplicate letters in the string? like, abcdd.

avatar
l*r
4
It asks to handle the case where there are duplicate letters in the input
string.
avatar
s*x
5
use or implement your own std::next_permutation
avatar
x*8
6

Easy way is use set, you cannot store duplicate keys in set :D
Or you can do some check, e.g aabc,
if(i>0&&str[i]==str[i-1]&&!flag[i-1]) continue;

【在 l********r 的大作中提到】
: It asks to handle the case where there are duplicate letters in the input
: string.

avatar
p*2
7
p = (x) -> console.log x
swap = (arr, i, j) -> [arr[i], arr[j]] = [arr[j], arr[i]]
dfs = (arr, curr) ->
if curr is arr.length
p arr.join("")
return
set = {}
for i in [curr...arr.length]
unless set[arr[i]]
set[arr[i]]=true
swap arr, curr, i
dfs arr, curr+1
swap arr, curr, i
permutation = (str) -> dfs str.split(""), 0
avatar
l*n
8
递归并用一个Boolean[26],记录是否用过某个字符作为开头了。

【在 l********r 的大作中提到】
: Old question, but can't find a good and easy to understand answer.
: What happens if there are duplicate letters in the string? like, abcdd.

avatar
h*o
9
我用swap那种解法理解的。去duplicate就加上noswap()判断。
bool noswap(int level, int i, const vector& num){
for (int j=level;jif (num[j]==num[i]){
return true;
}
}
return false;
}

void perm_recur(vector &num, int size, int level, vector> >& results) {
if (level == size) {results.push_back(num); return;}
for (int i = level; i < size; i++){
if (noswap(level, i, num)) continue;
swap(num[level], num[i]);
perm_recur(num, size, level+1, results);
swap(num[level], num[i]);
}
}

【在 l********r 的大作中提到】
: Old question, but can't find a good and easy to understand answer.
: What happens if there are duplicate letters in the string? like, abcdd.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。