nikunjsoni

17

Jun 24th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     const vector<string> pad = {
  4.         "", "", "abc", "def", "ghi", "jkl",
  5.         "mno", "pqrs", "tuv", "wxyz"
  6.     };
  7.    
  8.     vector<string> letterCombinations(string digits) {
  9.         if (digits.empty()) return {};
  10.         vector<string> result;
  11.         result.push_back("");
  12.        
  13.         for(auto digit: digits) {
  14.             vector<string> tmp;
  15.             for(auto candidate: pad[digit - '0']) {
  16.                 for(auto s: result) {
  17.                     tmp.push_back(s + candidate);
  18.                 }
  19.             }
  20.             result.swap(tmp);
  21.         }
  22.         return result;
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment