Skip to content

Commit

Permalink
212 1432ms 26.06%
Browse files Browse the repository at this point in the history
  • Loading branch information
mutoe committed Sep 19, 2021
1 parent 9dc2b61 commit 0b1f885
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
85 changes: 85 additions & 0 deletions 212_word_search_ii/word_search_ii.go
@@ -0,0 +1,85 @@
package word_search_ii

// https://leetcode.com/problems/word-search-ii

// level: 3
// time: unknown 1432ms 26.06%
// space: O(n) 2.6M 62.42%

//leetcode submit region begin(Prohibit modification and deletion)

type Node struct {
char byte
word string
next map[byte]*Node
}

type Trie struct {
root *Node
}

func Constructor() Trie {
root := &Node{byte(0), "", make(map[byte]*Node)}
return Trie{root}
}

func (t *Trie) Add(word string) {
cur := t.root
for i := 0; i < len(word); i++ {
char := word[i]
if cur.next[char] == nil {
cur.next[char] = &Node{char, "", make(map[byte]*Node)}
}
cur = cur.next[char]
}
cur.word = word
}

type Result struct {
board [][]byte
result map[string]bool
visited map[[2]int]bool
}

func (r *Result) search(x, y int, node *Node) {
if x < 0 || y < 0 || y >= len(r.board) || x >= len(r.board[0]) || r.visited[[2]int{x, y}] {
return
}
r.visited[[2]int{x, y}] = true
for char, n := range node.next {
if char != r.board[y][x] {
continue
}
r.search(x+1, y, n)
r.search(x-1, y, n)
r.search(x, y+1, n)
r.search(x, y-1, n)

if n.word != "" {
r.result[n.word] = true
continue
}
}
r.visited[[2]int{x, y}] = false
}

func findWords(board [][]byte, words []string) []string {
t := Constructor()
for _, word := range words {
t.Add(word)
}
r := &Result{board, map[string]bool{}, map[[2]int]bool{}}
for j := 0; j < len(board); j++ {
for i := 0; i < len(board[j]); i++ {
r.search(i, j, t.root)
}
}
ret, i := make([]string, len(r.result)), 0
for s := range r.result {
ret[i] = s
i++
}
return ret
}

//leetcode submit region end(Prohibit modification and deletion)
76 changes: 76 additions & 0 deletions 212_word_search_ii/word_search_ii_test.go
@@ -0,0 +1,76 @@
package word_search_ii

import (
"reflect"
"sort"
"testing"
)

func Test_findWords(t *testing.T) {
type args struct {
board [][]byte
words []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "test1",
args: args{[][]byte{
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'},
}, []string{"oath", "pea", "eat", "rain"}},
want: []string{"eat", "oath"},
},
{
name: "test2",
args: args{[][]byte{
{'a', 'b'},
{'c', 'd'},
}, []string{"abcd"}},
want: []string{},
},
{
name: "test3",
args: args{[][]byte{{'a'}}, []string{"a"}},
want: []string{"a"},
},
{
name: "test4",
args: args{[][]byte{
{'o', 'a', 'b', 'n'},
{'o', 't', 'a', 'e'},
{'a', 'h', 'k', 'r'},
{'a', 'f', 'l', 'v'},
}, []string{"oa", "oaa"}},
want: []string{"oa", "oaa"},
},
{
name: "test5",
args: args{[][]byte{{'a', 'a'}}, []string{"aaa"}},
want: []string{},
},
{
name: "test6",
args: args{[][]byte{
{'a', 'b'},
{'c', 'd'},
}, []string{"abdca"}},
want: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := findWords(tt.args.board, tt.args.words)
sort.Strings(got)
sort.Strings(tt.want)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("findWords() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 0b1f885

Please sign in to comment.