The question is rather a sporting interest, rather than something very much needed
In general, there is a rather banal algorithm for generating various permutations of a string with factorial complexity.
void permutations(std::string s, int pos = 0) { if (pos >= s.size()) { std::cout << s << '\n'; return; } else { for (int i = pos; i < s.size(); ++i) { std::swap(s[i], s[pos]); permutations(s, pos + 1); std::swap(s[i], s[pos]); } } } It is known from theory that any recursion can be rewritten iteratively using stack emulation.
update how to do this in this case without using any data structures to emulate a call stack?