Skip to content

Commit

Permalink
Problem 172: letter combinations of digits of a dialpad
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Jan 15, 2018
1 parent 924cf8b commit cd1585b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 171 |
| Total Problems | 172 |

</center>

Expand Down Expand Up @@ -206,6 +206,11 @@ Include contains single header implementation of data structures and some algori
| 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)|
| 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)|

### Backtracking Problems
| Problem | Solution |
| :------------ | :----------: |
| 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)|


### Leet code Problems
| Problem | Solution |
Expand Down
80 changes: 80 additions & 0 deletions backtracking_problems/dialpad_combinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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".
* Example: "34" will give output: {"dg","dh","di","eg","eh","ei","fg","fh","fi"}
* Note that order does not matter in result set.
*
* Approach:
*
* We can use a simple backtracking approach. Since each digit letter could be mapped to multiple characters,
* for example '1' is mapped to 'a', 'b' and 'c'. As we move along in the digit string, for an index i, we will
* add all possible letters to the string and recursively move ahead to i+1,
* i.e. we will choose say 'a' as our choice for the iteration, we map '1' to 'a' and then move to next
* digit letter (i+1) and recurse, then we will make 'b' as our choice for position i, and then 'c'
* This way we do it for all the letters at all the positions.
* The idea is to generate all possible subsets of the string, keeping in mind the order.
*/

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

std::unordered_map<char, std::string> dict {
{'1' , "" },
{'2' , "abc"},
{'3' , "def"},
{'4' , "ghi"},
{'5' , "jkl"},
{'6' , "mno"},
{'7' , "pqrs"},
{'8' , "tuv"},
{'9' , "wxyz"},
{'0' , " "},
{'*' , "*"},
{'#' , "#"}
};

void helper(const std::string& digits, std::string rs,
std::vector<std::string>& result, int index)
{
if (index == digits.length()) {
result.push_back(rs);
return;
}

char c = digits[index];
std::string ps = dict[c];
for (int i = 0; i < ps.length(); ++i) {
helper(digits, rs + ps[i], result, index + 1);
}
}

std::vector<std::string> letter_combinations(const std::string& digits)
{
std::vector<std::string> result;
if (digits.size() == 0) {
return result;
}
std::string rs("");
helper(digits, rs, result, 0);
return result;
}

void print_vec(const std::vector<std::string>& vec)
{
for(auto v: vec) {
std::cout << v << " ";
}
std::cout << std::endl;
}
int main()
{
std::string digits{"34"};
std::cout << "All possible combinations of digits " << digits << " are: \n";
print_vec(letter_combinations(digits));
return 0;
}

0 comments on commit cd1585b

Please sign in to comment.