-
Notifications
You must be signed in to change notification settings - Fork 0
/
17 Letter Combinations of a Phone Number.cpp
102 lines (89 loc) · 2.71 KB
/
17 Letter Combinations of a Phone Number.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
int stoi(char c){
return c-'0';
}
vector<string> letterCombinations(string digits) {
vector<string>ans;
int n=digits.length();
// cout<<n;
if(n==0)
return ans;
vector<string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
// for(int i=0;i<v.size();++i){
// cout<<v[i]<<endl;
// }
// cout<<v[2]<<endl;
bool flag=true;
queue<string>q1,q2;
// i assume q1 already contains the digits[0] string characters, q2 is empty
string pre=v[stoi(digits[0])];
// cout<<endl<<pre<<endl<<digits[0];
string new_st;
// cout<<endl<<new_st;
for(int j=0;j<pre.length();++j){
new_st="";
new_st.push_back(pre[j]);
q1.push(new_st);
}
// cout<<endl;
// while(!q1.empty()){
// cout<<q1.front()<<" ";
// q1.pop();
// }
int i=1;// i=1
// initially q2 is empty
while(i<n){
string temp=v[stoi(digits[i])];
if(flag){
while(!q1.empty()){
for(int j=0;j<temp.length();++j){
cout<<temp[j]<<" ";
new_st=q1.front();
new_st.push_back(temp[j]);
// string new_st=new_st+q1.front();
cout<<new_st<<" ";
q2.push(new_st);
}
q1.pop();
}
flag=false;
// after this q1 is empty
}
else{
while(!q2.empty()){
for(int j=0;j<temp.length();++j){
new_st=q2.front();
new_st.push_back(temp[j]);
// string new_st=new_st+q2.front();
q1.push(new_st);
}
q2.pop();
}
flag=true;
// after this q2 is empty
}
++i;
}
if(flag){
while(!q1.empty()){
ans.push_back(q1.front());
q1.pop();
}
}
else{
while(!q2.empty()){
ans.push_back(q2.front());
q2.pop();
}
}
return ans;
// O(4*4*4*4*4)=O(1024)
}
};