-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution17.cs
48 lines (41 loc) · 1.17 KB
/
Solution17.cs
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
namespace LeetCode.Solutions;
public class Solution17
{
/// <summary>
/// Problem #17
/// <a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">See the problem</a>
/// </summary>
public IList<string> LetterCombinations(string digits)
{
if (string.IsNullOrEmpty(digits))
{
return [];
}
Dictionary<char, string> lettersMap = new()
{
{ '2', "abc" },
{ '3', "def" },
{ '4', "ghi" },
{ '5', "jkl" },
{ '6', "mno" },
{ '7', "pqrs" },
{ '8', "tuv" },
{ '9', "wxyz" }
};
var result = new List<string>() { "" };
foreach (var digit in digits)
{
var letters = lettersMap[digit];
var newCombinations = new List<string>();
foreach (var combination in result)
{
foreach (var letter in letters)
{
newCombinations.Add(combination + letter);
}
}
result = newCombinations;
}
return result;
}
}