-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLetterCombinationsofaPhoneNumber.cs
executable file
·76 lines (73 loc) · 4.82 KB
/
LetterCombinationsofaPhoneNumber.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Source : https://leetcode.com/problems/letter-combinations-of-a-phone-number/
// Author : codeyu
// Date : Thursday, October 6, 2016 11:57:53 AM
/***************************************************************************************
*
* Given a digit string, return all possible letter combinations that the number could represent.
*
*
*
* A mapping of digit to letters (just like on the telephone buttons) is given below.
* _________________________
* | 1(o_o) 2(abc) 3(def) |
* | 4(ghi) 5(jkl) 6(mno) |
* | 7(pqrs) 8(tuv) 9(wxyz) |
* | *(+) 0(︺) ⬆️(#) |
* ——————————————————————————
* Input:Digit string "23"
* Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
*
*
*
* Note:
* Although the above answer is in lexicographical order, your answer could be in any order you want.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace Algorithms
{
public class Solution017
{
public static IList<string> LetterCombinations(string digits)
{
List<string> result = new List<string>();
if(string.IsNullOrEmpty(digits))
{
return result;
}
Dictionary<char,char[]> dict = new Dictionary<char,char[]>
{
{'0',new char[]{}},
{'1',new char[]{}},
{'2',new[]{'a','b','c'}},
{'3',new[]{'d', 'e', 'f'}},
{'4', new [] { 'g', 'h', 'i' }},
{'5', new [] { 'j', 'k', 'l' }},
{'6', new [] { 'm', 'n', 'o' }},
{'7', new [] { 'p', 'q', 'r', 's' }},
{'8', new [] { 't', 'u', 'v'}},
{'9', new [] { 'w', 'x', 'y', 'z' }}
};
StringBuilder s = new StringBuilder();
Helper(result, dict, s, digits,0);
return result;
}
private static void Helper(List<String> result, Dictionary<char, char[]> dict, StringBuilder s, String digits, int i )
{
if (digits.Length == s.Length)
{
result.Add(s.ToString());
return;
}
foreach (char c in dict[digits[i]] )
{
s.Append(c);
Helper(result, dict, s, digits, i + 1);
s.Remove(s.Length - 1, 1);
}
}
}
}