Just finish this coding, idea taken from @dream_machine
Basically, this is a backtracking algorithm, complexity is O (2n!). You need to track left to know if the line should output.
It seems that the algorithm is too slow, you may need to add a note to speed it up.
void helper(int start, string &s, string &path, vector<string> &result, int); vector<string> getPossibleCombo(string &s) { vector<string> result; string path; helper(0, s, path, result, s.size()); return result; } void helper(int start, string &s, string &path, vector<string> &result, int left) { if (start == s.size() && left == 0) { result.push_back(path); return; } for (int i = start; i < s.size(); i++) { path.push_back('a' + (s[i] - '0') - 1); helper(i + 1, s, path, result, left - 1); path.pop_back(); if (i < s.size() - 1 && s[i] > '0' && s[i] <= '2') {
output
bash-3.2$ g++ -std=c++11 -g test2.cpp && ./a.out abcde awde lcde
source share