Skip to content

Commit 6373c65

Browse files
committed
all permutation of string
1 parent b7a5e19 commit 6373c65

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
void permute(string s, int low, int high, set<string>& ans) {
2+
if (low == high) {
3+
ans.insert(s);
4+
return;
5+
}
6+
for (int i = low; i <= high; i++) {
7+
swap(s[low], s[i]);
8+
permute(s, low + 1, high, ans);
9+
swap(s[low], s[i]); // backtract to original str
10+
}
11+
12+
}
13+
vector<string>find_permutation(string S)
14+
{
15+
set<string> temp;
16+
permute(S, 0, S.size() - 1, temp);
17+
vector<string> ans(temp.begin(), temp.end());
18+
return ans;
19+
}

0 commit comments

Comments
 (0)