-
Notifications
You must be signed in to change notification settings - Fork 0
/
017.go
49 lines (44 loc) · 966 Bytes
/
017.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
49
package p017
/**
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.
*/
var (
i2cMap = map[byte]string{
'1': "",
'2': "abc",
'3': "def",
'4': "ghi",
'5': "jkl",
'6': "mno",
'7': "pqrs",
'8': "tuv",
'9': "wxyz",
}
)
func letterCombinations(digits string) []string {
return bytesCombinations([]byte(digits))
}
func bytesCombinations(digits []byte) []string {
ans := make([]string, 0)
if len(digits) == 0 {
return ans
}
if len(digits) == 1 {
chars := i2cMap[digits[0]]
for _, v := range []byte(chars) {
ans = append(ans, string(v))
}
return ans
} else {
latters := bytesCombinations(digits[1:])
chars := i2cMap[digits[0]]
for _, v := range []byte(chars) {
//ans = append(ans, string(v))
for _, latter := range latters {
ans = append(ans, string(v)+latter)
}
}
return ans
}
}