-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode Problem 17 Letter Combinations of a phone number.txt
57 lines (47 loc) · 1.74 KB
/
Leetcode Problem 17 Letter Combinations of a phone number.txt
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
Leetcode Problem 17 : Letter Combinations of a Phone Number
Description: Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.
Example : Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Hint to solve: recrusive solution until no digit left.
Language: C#
public class Solution
{
IList<string> Answer;
List<string> mapping;
public Solution()
{
Answer = new List<string>();
mapping = new List<string>{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
}
public void GeneratePermutaion(string digitsLeft,string currentCombination)
{
// Console.WriteLine("DigitsLeft: "+ digitsLeft + " currentCombination: "+currentCombination);
if(String.IsNullOrEmpty(digitsLeft))
{
Answer.Add(currentCombination);
return;
}
char firstDigit = digitsLeft[0];
string temp = "";
for(int i = 1; i<digitsLeft.Count(); ++i)
{
temp += digitsLeft[i];
}
digitsLeft = temp;
string alphabets = mapping[firstDigit - '0'];
for(int i = 0; i<alphabets.Count(); ++i)
{
string combination = currentCombination + alphabets[i];
GeneratePermutaion(digitsLeft,combination);
}
}
public IList<string> LetterCombinations(string digits)
{
if(String.IsNullOrEmpty(digits))
return Answer;
string combination = "";
GeneratePermutaion(digits,combination);
return Answer;
}
}