-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17LetterCombinationOfAPhoneNumber.cs
77 lines (67 loc) · 1.92 KB
/
17LetterCombinationOfAPhoneNumber.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
76
77
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LetterCombinationOfAPhoneNumber
{
class LetterCombinationOfAPhoneNumber
{
static void Main(string[] args)
{
ArrayList test = letterCombinations("23");
}
/*
*
* http://codeganker.blogspot.ca/2014/02/letter-combinations-of-phone-number.html
*/
public static ArrayList letterCombinations(String digits)
{
ArrayList res = new ArrayList();
res.Add("");
if (digits == null || digits.Length == 0)
return res;
for (int i = 0; i < digits.Length; i++)
{
String letters = getLetters(digits[i]);
ArrayList newRes = new ArrayList();
for (int j = 0; j < res.Count; j++)
{
for (int k = 0; k < letters.Length; k++)
{
newRes.Add(res[j] + letters[k].ToString());
}
}
res = newRes;
}
return res;
}
private static String getLetters(char digit)
{
switch (digit)
{
case '2':
return "abc";
case '3':
return "def";
case '4':
return "ghi";
case '5':
return "jkl";
case '6':
return "mno";
case '7':
return "pqrs";
case '8':
return "tuv";
case '9':
return "wxyz";
case '0':
return " ";
default:
return "";
}
}
}
}