Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 523c554

Browse files
committed
feat(solution): Added solution for 0017
1 parent 0a63669 commit 523c554

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

0001-1000.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Algorithms <!-- omit from toc -->
44

55
- [Array](#array)
6+
- [Backtracking](#backtracking)
67
- [Binary Search](#binary-search)
78
- [Dynamic Programming](#dynamic-programming)
89
- [Greedy](#greedy)
@@ -16,6 +17,12 @@
1617
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
1718
|1672|[Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/)| [C#](./C%23/1672-Richest_Customer_Wealth.cs)| $O(n \cdot m)$| $O(1)$|Easy|||
1819

20+
## Backtracking
21+
22+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
23+
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
24+
|0017|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [C#](./C%23/0017-Letter_Combinations_of_a_Phone_Number.cs)| $O(4^n)$| $O(4^n)$|Medium|||
25+
1926
## Binary Search
2027

2128
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// URL: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
2+
// Time Complexity: O(4^n)
3+
// Space Complexity: O(4^n)
4+
5+
public class Solution
6+
{
7+
public IList<string> LetterCombinations(string digits)
8+
{
9+
if (string.IsNullOrEmpty(digits)) return [];
10+
11+
Dictionary<string, string> map = new Dictionary<string, string>(){
12+
{"2", "abc"},
13+
{"3", "def"},
14+
{"4", "ghi"},
15+
{"5", "jkl"},
16+
{"6", "mno"},
17+
{"7", "qprs"},
18+
{"8", "tuv"},
19+
{"9", "wxyz"}
20+
};
21+
22+
IList<string> combinations = new List<string>();
23+
24+
void Backtracking(int index, string current)
25+
{
26+
if (current.Length == digits.Length)
27+
{
28+
combinations.Add(current);
29+
return;
30+
}
31+
32+
string chars = map[digits[index].ToString()];
33+
34+
for (int i = 0; i < chars.Length; i++)
35+
{
36+
Backtracking(index + 1, current + chars[i]);
37+
}
38+
}
39+
40+
Backtracking(0, "");
41+
42+
return combinations;
43+
}
44+
}

0 commit comments

Comments
 (0)