aboutsummaryrefslogtreecommitdiff
path: root/letter_combo_number.cpp
blob: c15a6281deb5dc327b9ab83c18bafdbe4351a47b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "leetcode.h"

class Solution {
public:
  vector<string> letterCombinations(string digits) {
    if (digits.empty())
      return {};
    vector<string> ans;
    ans.push_back("");
    for (auto digit : digits) {
      vector<string> tmp;
      for (auto candidate : nums[digit - '0'])
        for (auto s : ans)
          tmp.push_back(s + candidate);
      ans.swap(tmp);
    }
    for (int i = 0; i < digits.size(); i++)
      cout << ans[i];
    return ans;
  }

private:
  const vector<string> nums = {"",    "",    "abc",  "def", "ghi",
                               "jkl", "mno", "pqrs", "tuv", "wxyz"};
};

int main() {
  string digits = "23";
  cout << digits << endl;
  Solution obj;
  obj.letterCombinations(digits);
}