-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
35 lines (32 loc) · 957 Bytes
/
Solution.cs
File metadata and controls
35 lines (32 loc) · 957 Bytes
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
public class Solution
{
public IList<string> LetterCombinations(string digits)
{
var map = new Dictionary<char, List<string>>{
{'2',["a","b","c"]},
{'3',["d","e","f"]},
{'4',["g","h","i"]},
{'5',["j","k","l"]},
{'6',["m","n","o"]},
{'7',["p","q","r","s"]},
{'8',["t","u","v"]},
{'9',["w","x","y","z"]},
};
IList<string> ret = [];
BackTracking(0, "", digits, ret, map);
return ret;
}
void BackTracking(int pos, string candidate, string digits, IList<string> ret, Dictionary<char, List<string>> map)
{
if (digits.Length == 0) return;
if (pos >= digits.Length)
{
ret.Add(candidate);
return;
}
foreach (string letter in map[digits[pos]])
{
BackTracking(pos + 1, candidate + letter, digits, ret, map);
}
}
}