-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
48 lines (40 loc) · 798 Bytes
/
task.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 main
import (
"fmt"
"strings"
)
var numberRules = map[string]string{
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
func main() {
var input string
_, _ = fmt.Scan(&input)
fmt.Println(strings.Trim(fmt.Sprint(solution(input)), "[]"))
}
func solution(digits string) []string {
if digits == "" {
return nil
}
var result []string
var words = numberRules[digits[len(digits)-1:]]
var combinations = solution(digits[:len(digits)-1])
if len(combinations) > 0 {
for _, combination := range combinations {
for _, c := range strings.Split(words, "") {
result = append(result, combination+c)
}
}
} else {
for _, c := range strings.Split(words, "") {
result = append(result, c)
}
}
return result
}