fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. void getCombination(vector<string> &combinations, string current, const string &A, int idx, const vector<string> &keyset){
  6. if(idx >= A.size()) {
  7. combinations.push_back(current);
  8. return;
  9. }
  10. int key = (A[idx] - '0');
  11. int len = keyset[key].length();
  12.  
  13. for( int i = 0; i < len; i++ ){
  14. current.push_back(keyset[key][i]); //pick at once one char corresp. to that keypress
  15. getCombination(combinations, current, A, idx+1, keyset);
  16. current.pop_back();
  17. }
  18. }
  19.  
  20. vector<string> letterCombinations(string A) {
  21. vector<string> combinations;
  22. vector<string> keyset{"0", "1", "2abc", "3def", "4ghi", "5jkl", "6mno", "7pqrs", "8tuv", "9wxyz"};
  23. getCombination(combinations, std::string({}), A, 0, keyset);
  24. return combinations;
  25. }
  26.  
  27. int main() {
  28. vector<string> combinations = letterCombinations("23");
  29. for(string word : combinations){
  30. cout << word << ", ";
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
23, 2d, 2e, 2f, a3, ad, ae, af, b3, bd, be, bf, c3, cd, ce, cf,