MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / exist

Function exist

word_search_79/solution.go:9–19  ·  view source on GitHub ↗

Time: O(N * 4^(L (len of word))) Space: O(L) recursion stack at most L

(board [][]byte, word string)

Source from the content-addressed store, hash-verified

7// Time: O(N * 4^(L (len of word)))
8// Space: O(L) recursion stack at most L
9func exist(board [][]byte, word string) bool {
10 for r := 0; r < len(board); r++ {
11 for c := 0; c < len(board[r]); c++ {
12 if word[0] == board[r][c] && explore(board, word, 0, r, c) {
13 return true
14 }
15 }
16 }
17
18 return false
19}
20
21func explore(board [][]byte, word string, step, r, c int) bool {
22 // if the step is equal to the length of word, then word exists

Callers 1

Test_existFunction · 0.85

Calls 1

exploreFunction · 0.70

Tested by 1

Test_existFunction · 0.68