Letter Combinations of a Phone Number

 

CREATED TIME: Jan 05, 2020 10:05 PM UPDATED TIME: Jan 05, 2020 10:06 PM 난이도: Medium

#include <map>

class Solution {
public:
    map<int, vector<string>> m;
    Solution(){
        vector<string> tmp{"a", "b", "c"};
        m.insert({2, tmp});
        tmp =  vector<string>{"d", "e", "f"};
        m.insert({3, tmp});
        tmp = vector<string>{"g", "h", "i"};
        m.insert({4, tmp});
        tmp = vector<string>{"j", "k", "l"};
        m.insert({5, tmp});
        tmp = vector<string>{"m", "n", "o"};
        m.insert({6, tmp});
        tmp = vector<string>{"p", "q", "r", "s"};
        m.insert({7, tmp});
        tmp = vector<string>{"t", "u", "v"};
        m.insert({8, tmp});
        tmp = vector<string>{"w", "x", "y", "z"};
        m.insert({9, tmp});
    }
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        res = createSubString(digits);
        return res;
    }
    vector<string> createSubString(string digits){
        if(digits.length() == 0){
            vector<string> seed;
            return seed;
        }
        if(digits.length() == 1){
            vector<string> seed = m.find(int(digits.at(0)) - '0')->second;
            return seed;
        }
        
        vector<string> newSubStrings;
        vector<string> subStrings = createSubString(digits.substr(
            1, digits.size() - 1
        ));
        for(string c : m.find(int(digits[0]) - '0')->second){
            for(string s : subStrings){
                newSubStrings.push_back(c + s);
            }
        }
        return newSubStrings;
    }
};