MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / dfs

Method dfs

java/0212-word-search-ii.java:26–62  ·  view source on GitHub ↗
(
        int r,
        int c,
        Trie node,
        String word,
        HashSet<String> res,
        HashSet<String> visit,
        char[][] board,
        Trie root
    )

Source from the content-addressed store, hash-verified

24 }
25
26 public void dfs(
27 int r,
28 int c,
29 Trie node,
30 String word,
31 HashSet<String> res,
32 HashSet<String> visit,
33 char[][] board,
34 Trie root
35 ) {
36 if (
37 r < 0 ||
38 c < 0 ||
39 r == ROWS ||
40 c == COLS ||
41 !node.children.containsKey(board[r][c]) ||
42 node.children.get(board[r][c]).refs < 1 ||
43 visit.contains(r + "-" + c)
44 ) {
45 return;
46 }
47
48 visit.add(r + "-" + c);
49 node = node.children.get(board[r][c]);
50 word += board[r][c];
51 if (node.isWord) {
52 node.isWord = false;
53 res.add(word);
54 root.removeWord(word);
55 }
56
57 dfs(r + 1, c, node, word, res, visit, board, root);
58 dfs(r - 1, c, node, word, res, visit, board, root);
59 dfs(r, c + 1, node, word, res, visit, board, root);
60 dfs(r, c - 1, node, word, res, visit, board, root);
61 visit.remove(r + "-" + c);
62 }
63
64 class Trie {
65

Callers 1

findWordsMethod · 0.95

Calls 5

getMethod · 0.45
containsMethod · 0.45
addMethod · 0.45
removeWordMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected