-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintPhoneDigits.cs
60 lines (45 loc) · 1.37 KB
/
PrintPhoneDigits.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
using System;
using System.Collections;
using System.Collections.Generic;
// To execute C#, please define "static void Main" on a class
// named Solution.
class Solution
{
static void Main(string[] args)
{
String str = "23";
List<string> results = PhoneWordGenerator.PrintKeywords(str);
foreach(string res in results)
{
Console.Write(res + " ");
}
}
}
public class PhoneWordGenerator
{
public static string[] codes = new string[] {"", "ABC", "DEF", "GHI",
"JKL", "MNO", "PQR", "STU",
"VWX", "YZ"};
public static List<string> PrintKeywords(string str)
{
if(str.Length == 0)
{
List<string> baseRes = new List<string>();
baseRes.Add(" ");
return baseRes;
}
char ch = str[0];
string restStr = str.Substring(1);
List<string> prevRes = PrintKeywords(restStr);
List<string> res = new List<string>();
string code = codes[ch - '0'];
foreach(string s in prevRes)
{
for(int i=0; i<code.Length; i++)
{
res.Add(code[i] + s);
}
}
return res;
}
}