-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 31
69 lines (57 loc) · 1.71 KB
/
Day 31
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
Que:- https://leetcode.com/problems/letter-combinations-of-a-phone-number
Case 1:
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Case 2:
Input: digits = ""
Output: []
Case 3:
Input: digits = "2"
Output: ["a","b","c"]
Constraints:
0 <= digits.length <= 4
digits[i] is a digit in the range ['2', '9'].
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ans;
int n=digits.length();
if(n==0){
return ans;
}
map<string,vector<string>> m;
m["2"]={"a","b","c"};
m["3"]={"d","e","f"};
m["4"]={"g","h","i"};
m["5"]={"j","k","l"};
m["6"]={"m","n","o"};
m["7"]={"p","q","r","s"};
m["8"]={"t","u","v"};
m["9"]={"w","x","y","z"};
for(int i=0;i<digits.length();i++){
if(i==0){
string index;
index+=digits[i];
vector<string> curr=m[index];
for(int j=0;j<curr.size();j++){
ans.push_back(curr[j]);
}
}else{
string index;
index+=digits[i];
vector<string> curr=m[index];
// add every char of current to every string of ans
vector<string> curr_ans;
for(int j=0;j<curr.size();j++){
string curr_char=curr[j];
for(int k=0;k<ans.size();k++){
string new_string=ans[k]+curr_char;
curr_ans.push_back(new_string);
}
}
ans=curr_ans;
}
}
return ans;
}
};