jayati

Letter Combinations of a Phone Number

Jan 31st, 2023
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. class Solution {
  2.  
  3. void solve(int index,string output,string digits,vector<string> &v,unordered_map<char,string> m)
  4. {
  5.     if(index>=digits.length())
  6.     {
  7.         v.push_back(output);
  8.         return;
  9.     }
  10.     string val = m[digits[index]];
  11.     for(int i=0;i<val.length();i++)
  12.     {
  13.         output.push_back(val[i]);
  14.         solve(index+1,output,digits,v,m);
  15.         output.pop_back();
  16.     }
  17.    
  18. }
  19.  
  20. public:
  21.     vector<string> letterCombinations(string digits) {
  22.         unordered_map<char,string> m;
  23.         m['2']="abc";
  24.         m['3']="def";
  25.         m['4']="ghi";
  26.         m['5']="jkl";
  27.         m['6']="mno";
  28.         m['7']="pqrs";
  29.         m['8']="tuv";
  30.         m['9']="wxyz";
  31.         vector<string> v;
  32.         if(digits.length()==0)
  33.         {
  34.             return v;
  35.         }
  36.         int index=0;
  37.         string output="";
  38.         solve(index,output,digits,v,m);
  39.  
  40.         return v;
  41.     }
  42. };
Advertisement
Add Comment
Please, Sign In to add comment