Created
October 27, 2018 00:20
-
-
Save zhangxiaomu01/fdbbdca66f8eb01e42cd790c044bc9e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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