Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 12881a2

Browse files
17. Letter Combinations of a Phone Number in C++ (#216)
* Letter Combinations of a Phone Number added * Update README.md * Update README.md
1 parent da168da commit 12881a2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
unordered_map<char,string> intToCharsMap;
4+
5+
void backtracking(string::iterator lf,string::iterator rt,string &path,vector<string> &result)
6+
{
7+
if(lf == rt)
8+
{
9+
result.push_back(path);
10+
return;
11+
}
12+
for(auto c : intToCharsMap[*lf])
13+
{
14+
path.push_back(c);
15+
backtracking(next(lf,1),rt,path,result);
16+
path.pop_back(); // if a character doesnot matches then we pop that character from the string and again backtrack.
17+
}
18+
}
19+
vector<string> letterCombinations(string digits)
20+
{
21+
int n = digits.size();
22+
if(digits == "")
23+
return {};
24+
string path;
25+
// result array stores every string that represents that digit.
26+
vector<string> result;
27+
intToCharsMap = {
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+
};
37+
backtracking(digits.begin(),digits.end(),path,result);
38+
return result;
39+
}
40+
};

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
364364
| 037 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) | [C++](./C++/Sudoku-Solver.cpp) | _O(n^2)_ | _O(1)_ | Hard | Hash Table | |
365365
| 980 | [Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | [C++](./C++/Unique-Paths-III.cpp) | _O(R * C * 2 ^ (R \* C))_ | _O(R \* C)_ | Hard | DFS, Memoization | |
366366
| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [C++](./C++/combination-sum.cpp) | _O(2^n)_ | _O(n)_ | Medium | Array, Backtracking | |
367+
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [C++](./C++/letter-combinations-of-a-phone-number.cpp) | _O(4^n)_ | _O(n)_ | Medium | String, Hash Table, Backtracking | |
367368

368369
<br/>
369370
<div align="right">
@@ -502,6 +503,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
502503
| [Sachin_Upadhyay](https://github.com/sachsbu) <br> <img src="https://avatars.githubusercontent.com/u/24941685?v=4" width="100" height="100"> | India | Java | [GitHub](https://github.com/sachsbu)
503504
| [Amisha Sahu](https://github.com/Amisha328) <br> <img src = "https://avatars.githubusercontent.com/u/58816552?v=4" width="100" height="100"> | India | C++ | [CodeChef](https://www.codechef.com/users/amisha328)<br/>[LeetCode](https://leetcode.com/Mishi328/)<br/>[HackerRank](https://www.hackerrank.com/amishasahu328)
504505
| [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh)
506+
| [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408)
505507
<div align="right">
506508
<b><a href="#algorithms">⬆️ Back to Top</a></b>
507509
</div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /