I am a beginner of C++. I am trying to fix a permutation function. I know the bug here is chosen += ch
, it should be chosen + ch
, but I do not understand how chosen += ch
can be passed as a parameter here and what it means.
HashSet<string> permutationsRec(string str, string chosen) {
if (str == "") {
return { chosen };
}
else {
HashSet<string> result;
for (int i = 0; i < str.size(); i++) {
char ch = str[i];
string remaining = str.substr(0, i) + str.substr(i + 1);
HashSet<string> thisOption = permutationsRec(remaining, chosen += ch);
result += thisOption;
}
return result;
}
}