//17. Letter Combinations of a Phone Number
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
class Solution {
public:
vector<string> letterCombinations(string digits) {
res.clear();
if(digits==""){
return res;
}
findNext(digits,0,"");
return res;
}
private:
vector<string> res;
const string letterMaps[10] = {
" ", //0
"", //1
"abc", //2
"def", //3
"ghi", //4
"jkl", //5
"mno", //6
"pqrs", //7
"tuv", //8
"wxyz" //9
};
void findNext(const string &digits,int index,const string &s){
//const reference ,different const value
if(index==digits.size()){
res.push_back(s);
//return can't miss
return;
}
char c=digits[index];
cout<<c<<endl;
assert(c>='0' && c<='9' && c!='1');
string letters=letterMaps[c-'0'];
for(int i=0;i<letters.size();i++){
findNext(digits,index+1,s+letters[i]);
}
return;
}
};
int main(){
vector<string> res=Solution().letterCombinations("34");
for(int i=0;i<res.size();i++){
cout<<res[i]<<" ";
}
cout<<endl;
return 0;
}