Given a string, your task is to generate all different strings that can be created using its characters.
Example:
Input: s = "aabac"
Output:
20 aaabc aaacb aabac aabca aacab aacba abaac abaca abcaa acaab acaba acbaa baaac baaca bacaa bcaaa caaab caaba cabaa cbaaa
Approach:
C++
#include <bits/stdc++.h>using namespace std;void creatingStrings(string s){sort(s.begin(), s.end());set<string> st;do{st.insert(s);} while (next_permutation(s.begin(), s.end()));cout << st.size() << "\n";for (auto it = st.begin(); it != st.end(); it++)cout << *it << "\n";}int main(){string s = "aabac";creatingStrings(s);return 0;}
No comments:
Post a Comment