Skip to content

Commit cd1585b

Browse files
committed
Problem 172: letter combinations of digits of a dialpad
1 parent 924cf8b commit cd1585b

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 171 |
9+
| Total Problems | 172 |
1010

1111
</center>
1212

@@ -206,6 +206,11 @@ Include contains single header implementation of data structures and some algori
206206
| Given two integer arrays, A and B, each containing N integers. You are free to permute the order of the elements in the arrays. Is there an permutation A', B' possible of A and B, such that, A'<sub>i</sub>+B'<sub>i</sub> ≥ K for all i, where A'<sub>i</sub> denotes the i<sup>th</sup> element in the array A' and B'<sub>i</sub> denotes i<sup>th</sup> element in the array B'.| [two_arrays.cpp](greedy_problems/two_arrays.cpp)|
207207
| John is taking orders. The i<sup>th</sup> order is placed by the i<sup>th</sup> customer at t<sub>i</sub> time and it takes d<sub>i</sub> time to procees. What is the order in which the customers will get their orders? (see more details in solutions's comments)|[orders_order.cpp](greedy_problems/orders_order.cpp)|
208208

209+
### Backtracking Problems
210+
| Problem | Solution |
211+
| :------------ | :----------: |
212+
| You are given a digit string (e.g "1234", "567" etc), provide all possible letter combinations we could generate from this digit string, based on the mapping we see on the telphone/mobile dialpad. If you have typed SMS in old style phones, you would know. For e.g. "1" is mapped to "abc", 2 is mapped to "def". You can refer to the <a href="http://techotv.com/wp-content/uploads/2013/03/Lumia-620-dial-pad-screen-1.jpg" target="_blank">image.</a>. <ul><li> Example: "34" will give output: {"dg","dh","di","eg","eh","ei","fg","fh","fi"} </li></ul> Note that order does not matter in result set.|[dialpad_combinations.cpp](backtracking_problems/dialpad_combinations.cpp)|
213+
209214

210215
### Leet code Problems
211216
| Problem | Solution |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* You are given a digit string (e.g "1234", "567" etc),
3+
* provide all possible letter combinations we could generate from this digit string,
4+
* based on the mapping we see on the telphone/mobile dialpad.
5+
* If you have typed SMS in old style phones, you would know. For e.g. "1" is mapped to "abc",
6+
* 2 is mapped to "def".
7+
* Example: "34" will give output: {"dg","dh","di","eg","eh","ei","fg","fh","fi"}
8+
* Note that order does not matter in result set.
9+
*
10+
* Approach:
11+
*
12+
* We can use a simple backtracking approach. Since each digit letter could be mapped to multiple characters,
13+
* for example '1' is mapped to 'a', 'b' and 'c'. As we move along in the digit string, for an index i, we will
14+
* add all possible letters to the string and recursively move ahead to i+1,
15+
* i.e. we will choose say 'a' as our choice for the iteration, we map '1' to 'a' and then move to next
16+
* digit letter (i+1) and recurse, then we will make 'b' as our choice for position i, and then 'c'
17+
* This way we do it for all the letters at all the positions.
18+
* The idea is to generate all possible subsets of the string, keeping in mind the order.
19+
*/
20+
21+
#include <iostream>
22+
#include <string>
23+
#include <unordered_map>
24+
#include <vector>
25+
26+
std::unordered_map<char, std::string> dict {
27+
{'1' , "" },
28+
{'2' , "abc"},
29+
{'3' , "def"},
30+
{'4' , "ghi"},
31+
{'5' , "jkl"},
32+
{'6' , "mno"},
33+
{'7' , "pqrs"},
34+
{'8' , "tuv"},
35+
{'9' , "wxyz"},
36+
{'0' , " "},
37+
{'*' , "*"},
38+
{'#' , "#"}
39+
};
40+
41+
void helper(const std::string& digits, std::string rs,
42+
std::vector<std::string>& result, int index)
43+
{
44+
if (index == digits.length()) {
45+
result.push_back(rs);
46+
return;
47+
}
48+
49+
char c = digits[index];
50+
std::string ps = dict[c];
51+
for (int i = 0; i < ps.length(); ++i) {
52+
helper(digits, rs + ps[i], result, index + 1);
53+
}
54+
}
55+
56+
std::vector<std::string> letter_combinations(const std::string& digits)
57+
{
58+
std::vector<std::string> result;
59+
if (digits.size() == 0) {
60+
return result;
61+
}
62+
std::string rs("");
63+
helper(digits, rs, result, 0);
64+
return result;
65+
}
66+
67+
void print_vec(const std::vector<std::string>& vec)
68+
{
69+
for(auto v: vec) {
70+
std::cout << v << " ";
71+
}
72+
std::cout << std::endl;
73+
}
74+
int main()
75+
{
76+
std::string digits{"34"};
77+
std::cout << "All possible combinations of digits " << digits << " are: \n";
78+
print_vec(letter_combinations(digits));
79+
return 0;
80+
}

0 commit comments

Comments
 (0)