-
Notifications
You must be signed in to change notification settings - Fork 5
/
solution.go
56 lines (50 loc) · 1.14 KB
/
solution.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
54
55
56
package main
import (
"fmt"
"reflect"
)
var ans []string
var strs = []string{
"", "", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz",
}
func letterCombinations(digits string) []string {
ans = []string{}
if len(digits) == 0 {
return ans
}
dfs(digits, 0, "")
return ans
}
func dfs(digits string, idx int, combine string) {
if idx == len(digits) {
ans = append(ans, combine)
return
}
s := strs[digits[idx]-'0']
for i := 0; i < len(s); i++ {
combine += string(s[i])
dfs(digits, idx+1, combine)
combine = combine[:len(combine)-1]
}
}
type Example struct {
digits string
ans []string
}
func main() {
examples := []Example{
{"23", []string{"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"}},
{"", []string{}},
{"2", []string{"a", "b", "c"}},
}
for i, example := range examples {
ans := letterCombinations(example.digits)
if reflect.DeepEqual(ans, example.ans) {
fmt.Printf("PASS: CASE %d\n", i)
} else {
fmt.Printf("FAIL: CASE %d\n", i)
}
}
}