-
Notifications
You must be signed in to change notification settings - Fork 1
/
letter-combinations-of-a-phone-number.go
53 lines (40 loc) · 1.28 KB
/
letter-combinations-of-a-phone-number.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
50
51
52
53
package main
func letterCombinations(digits string) []string {
result := []string{}
if len(digits) == 0 {
return result
}
phone := map[byte][]string{}
phone[byte('2')] = []string{"a","b","c"}
phone[byte('3')] = []string{"d","e","f"}
phone[byte('4')] = []string{"g","h","i"}
phone[byte('5')] = []string{"j","k","l"}
phone[byte('6')] = []string{"m","n","o"}
phone[byte('7')] = []string{"p","q","r","s"}
phone[byte('8')] = []string{"t","u","v"}
phone[byte('9')] = []string{"w","x","y","z"}
// init
result = append(result, phone[digits[0]]...)
digits = digits[1:]
count := len(digits)
if count == 0 {
return result
}
for c := 0; c < count; c++ {
if digits[c] == '1' {
return []string{}
}
currentTotal := len(result)
total := currentTotal * len(phone[digits[c]])
delta := total - currentTotal
for k := 0; k < delta; {
for i := 0; i < currentTotal; i,k=i+1,k+1 {
result = append(result, result[i])
}
}
for i := 0; i < total; i++ {
result[i] += phone[digits[c]][i/currentTotal]
}
}
return result
}