Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class Solution {
  2. string dig_to_char[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
  3. void backtrack(string &digits, int cur, string &temp, vector<string> &ans)
  4. {
  5. if(temp.length()==digits.length())
  6. {
  7. ans.push_back(temp);
  8. return;
  9. }
  10. for(int i=0; i < dig_to_char[digits[cur]-'2'].size(); i++)
  11. {
  12. temp.push_back(dig_to_char[(digits[cur]-'2')][i]);
  13. backtrack(digits, cur+1, temp, ans);
  14. temp.pop_back();
  15. }
  16. }
  17. public:
  18. vector<string> letterCombinations(string digits)
  19. {
  20. if(digits=="")
  21. return {};
  22. vector<string> ans;
  23. string temp = "";
  24. backtrack(digits, 0, temp, ans);
  25. return ans;
  26. }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement