Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created October 27, 2018 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/fdbbdca66f8eb01e42cd790c044bc9e3 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/fdbbdca66f8eb01e42cd790c044bc9e3 to your computer and use it in GitHub Desktop.
class Solution {
private:
vector<string> strDic = {"", "", "abc", "def", "ghi","jkl", "mno","pqrs", "tuv", "wxyz"};
vector<string> finalVec;
public:
vector<string> letterCombinations(string digits) {
int len_d = digits.length();
for(int i = 0; i < len_d; i++){
int index = digits[i] - '0';
finalVec = multiStr(finalVec, getList(strDic[index]));
}
return finalVec;
}
vector<string> getList(string &a){
vector<string> ans;
for(int i = 0; i< a.length(); i++){
string temp = "";
temp = temp + a[i];
ans.push_back(temp);
}
return ans;
}
vector<string> multiStr(vector<string> &l1, vector<string> l2){
if(l1.size() == 0 && l2.size() != 0){
return l2;
}
if(l1.size() != 0 && l2.size() == 0){
return l1;
}
vector<string> ans;
for(int i = 0; i< l1.size(); i++){
for(int j = 0; j < l2.size(); j++){
ans.push_back(l1[i]+l2[j]);
}
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment