-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathLetterCombinationsOfAPhoneNumber.go
48 lines (39 loc) · 1.12 KB
/
LetterCombinationsOfAPhoneNumber.go
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
package LetterCombinationsOfAPhoneNumber
import "strconv"
//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.
//
//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.
//
//Accepted.
func letterCombinations(digits string) []string {
dict := []string{" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
results := make([]string, 0)
digitsLength := len(digits)
if digitsLength == 0 {
return results
}
if digitsLength == 1 {
i, _ := strconv.Atoi(digits)
for _, c := range dict[i] {
results = append(results, string(c))
}
return results
}
list := letterCombinations(string(digits[1:digitsLength]))
builder := ""
i, _ := strconv.Atoi(digits[0:1])
for _, c := range dict[i] {
for _, s := range list {
builder += string(c)
builder += s
results = append(results, builder)
builder = ""
}
}
return results
}