Leetcode#17 Letter Combinations of a Phone Number
Published:
Link:
https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
Idea:
Clone of existing array elements.
Solution:
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> lettComb;
if (digits.size() == 0)
return lettComb;
lettComb.push_back("");
string dict[] = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for(int i=0; i<digits.size(); i++) {
int n = lettComb.size();
string lett = dict[digits[i]-'0'];
for(int j=0; j<n; j++) {
for(int k=1; k<lett.size(); k++) {
string comb = lettComb[j]; //clone lettComb[j]
comb.push_back(lett[k]);
lettComb.push_back(comb);
}
lettComb[j].push_back(lett[0]);
}
}
return lettComb;
}
};