topic:
Given a string containing only the numbers 2-9, return all letter combinations it can represent. The answers can be returned in any order.
The mapping of numbers to letters is given as follows (same as phone buttons). Note that 1 does not correspond to any letters.
Example:
**Input: **digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
solution
Code
class Solution {
private:
const string letterMap[10] = {
"", // 0
"", // 1
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz", // 9
};
public:
vector<string> result;
void getCombinations(const string& digits, int index, const string& s) {
if (index == digits.size()) {
result.push_back(s);
return;
}
int digit = digits[index] - '0';
string letters = letterMap[digit];
for (int i = 0; i < letters.size(); i++) {
getCombinations(digits, index + 1, s + letters[i]);
}
}
vector<string> letterCombinations(string digits) {
result.clear();
if (digits.size() == 0) {
return result;
}
getCombinations(digits, 0, "");
return result;
}
};