-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathphone_keyword.cpp
46 lines (43 loc) · 1.18 KB
/
phone_keyword.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Letter Combinations of a Phone Number
// codelink :: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
/*for ex -
i/p : 23
o/p : ad,ae,af,bd,be,bf,cd,ce,cf
*/
#include<iostream>
#include<vector>
using namespace std;
void solve(string digits, string output, int index, vector<string> &ans,string mapping[]){
//base case
if (index>=digits.length()){
ans.push_back(output);
return;
}
int number = digits[index] - '0';
string value = mapping[number];
for(int i =0;i<value.length();i++){
output.push_back(value[i]);
solve(digits,output,index+1,ans,mapping);
output.pop_back();
}
}
vector<string> letterCombinations(string digits) {
vector<string> ans;
if(digits.length() == 0){
return ans;
}
string output = "";
int index = 0;
string mapping[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
solve(digits,output,index,ans,mapping);
return ans;
}
int main(){
string s;
cin >> s;
vector<string> ans = letterCombinations(s);
for(int i=0;i<ans.size();i++){
cout << ans[i] <<", ";
}
return 0;
}