Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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