-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
77 lines (66 loc) · 1.58 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"strings"
)
func getMappedLetters(char rune) []string {
letterMap := map[string][]string{
"2": {"a", "b", "c"},
"3": {"d", "e", "f"},
"4": {"g", "h", "i"},
"5": {"j", "k", "l"},
"6": {"m", "n", "o"},
"7": {"p", "q", "r", "s"},
"8": {"t", "u", "v"},
"9": {"w", "x", "y", "z"},
}
return letterMap[string(char)]
}
func copy(s []string) []string {
t := make([]string, 0, len(s))
t = append(t, s...)
return t
}
func remove(s []string, num string) []string {
index := -1
for i, v := range s {
if v == num {
index = i
}
}
return append(s[:index], s[index+1:]...)
}
func letterCombinations(digits string) []string {
listOfLetters := make([][]string, 0, len(digits))
for _, item := range digits {
listOfLetters = append(listOfLetters, getMappedLetters(item))
}
fmt.Printf("list of letters %v\n", listOfLetters)
result := make([]string, 0)
result = solutionFinder(listOfLetters, result, []string{}, 0)
return result
}
func solutionFinder(listOfLetters [][]string, result []string, cur []string, index int) []string {
fmt.Println(cur, index, result)
if index > len(listOfLetters) {
return result
}
if len(cur) == len(listOfLetters) {
if len(cur) > 0 {
result = append(result, strings.Join(cur, ""))
}
fmt.Println(result, cur)
return result
}
for i := 0; i < len(listOfLetters[index]); i++ {
ch := listOfLetters[index][i]
cur = append(cur, ch)
result = solutionFinder(listOfLetters, result, cur, index+1)
cur = remove(cur, ch)
}
return result
}
func main() {
digits := ""
fmt.Println(letterCombinations(digits))
}