All Subsets I (LaiCode)

class Solution { public: void solveHelper(string input, size_t pos, string & chosen, vector<string> & allsubsets){ if (pos == input.size()){ allsubsets.push_back(chosen); return; } // else: choose, explore, unchoose...The choice is whether to include char at this position or not // 1. do not choose char at this position solveHelper(input, pos+1, chosen, allsubsets); // 2. Do choose char at this position chosen.push_back(input[pos]); solveHelper(input, pos+1, chosen, allsubsets); chosen.pop_back(); } vector<string> solve(string input) { string chosen = ""; vector<string> allsubsets = {}; solveHelper(input, 0, chosen, allsubsets); return allsubsets; } };
Here we use a string...
Forget to use return in the recursive call of helper function.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.