leetcode: Letter Combinations of a Phone Number
A mapping of digit to letters (just like on the telephone buttons) is given below.
vector
letterCombinations(string digits) {
vector
result;
vector
next_result;
if (digits.size() >1 )
{
next_result = letterCombinations(digits.substr(1, digits.size()-1));
}
if (digits[0] > '1' && digits[0]<= '9')
{
char current[2] = {(digits[0] - '2')*3 + 'a', '\0'};
strstream s;
for (int j = 0; j < 3; ++j)
{
string temp(current);
if (next_result.size() == 0)
result.push_back(temp);
for (int i = 0; i < next_result.size(); ++i)
{
result.push_back(temp+ next_result[i]);
}
current[0] += 1;
}
}else if (digits[0] == '0' ){
char current[2] = {' ', '\0'};
strstream s;
string temp;
for (int j = 0; j < 3; ++j)
{
s<
>temp;
if (next_result.size() == 0)
result.push_back(temp);
for (int i = 0; i < next_result.size(); ++i)
{
result.push_back(temp+ next_result[i]);
}
current[0] += 1;
}
}
return result;
}