public List<String> findWords(char[][] board, String[] words) { Trietrie=newTrie(); for (String word : words) { trie.insert(word); }
Set<String> ans = newHashSet<String>(); for (inti=0; i < board.length; ++i) { for (intj=0; j < board[0].length; ++j) { dfs(board, trie, i, j, ans); } }
returnnewArrayList<String>(ans); }
publicvoiddfs(char[][] board, Trie now, int i1, int j1, Set<String> ans) { if (!now.children.containsKey(board[i1][j1])) { return; } charch= board[i1][j1]; now = now.children.get(ch); if (!"".equals(now.word)) { ans.add(now.word); }
public IList<string> FindWords(char[][] board, string[] words) { Trie trie = new Trie(); foreach (string word in words) { trie.Insert(word); }
ISet<string> ans = new HashSet<string>(); for (int i = 0; i < board.Length; ++i) { for (int j = 0; j < board[0].Length; ++j) { DFS(board, trie, i, j, ans); } }
returnnew List<string>(ans); }
voidDFS(char[][] board, Trie now, int i1, int j1, ISet<string> ans) { if (!now.children.ContainsKey(board[i1][j1])) { return; } char ch = board[i1][j1]; now = now.children[ch]; if (!"".Equals(now.word)) { ans.Add(now.word); }
publicvoidInsert(string word) { Trie cur = this; foreach (char c in word) { if (!cur.children.ContainsKey(c)) { cur.children.Add(c, new Trie()); } cur = cur.children[c]; } cur.word = word; } }
type Trie struct { children [26]*Trie word string }
func(t *Trie) Insert(word string) { node := t for _, ch := range word { ch -= 'a' if node.children[ch] == nil { node.children[ch] = &Trie{} } node = node.children[ch] } node.word = word }
var dirs = []struct{ x, y int }{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
funcfindWords(board [][]byte, words []string) []string { t := &Trie{} for _, word := range words { t.Insert(word) }
m, n := len(board), len(board[0]) seen := map[string]bool{}
var dfs func(node *Trie, x, y int) dfs = func(node *Trie, x, y int) { ch := board[x][y] node = node.children[ch-'a'] if node == nil { return }
if node.word != "" { seen[node.word] = true }
board[x][y] = '#' for _, d := range dirs { nx, ny := x+d.x, y+d.y if0 <= nx && nx < m && 0 <= ny && ny < n && board[nx][ny] != '#' { dfs(node, nx, ny) } } board[x][y] = ch } for i, row := range board { for j := range row { dfs(t, i, j) } }
ans := make([]string, 0, len(seen)) for s := range seen { ans = append(ans, s) } return ans }
booldfs(vector<vector<char>>& board, int x, int y, TrieNode * root, set<string> & res){ char ch = board[x][y]; if (!root->children.count(ch)) { returnfalse; } root = root->children[ch]; if (root->word.size() > 0) { res.insert(root->word); }
board[x][y] = '#'; for (int i = 0; i < 4; ++i) { int nx = x + dirs[i][0]; int ny = y + dirs[i][1]; if (nx >= 0 && nx < board.size() && ny >= 0 && ny < board[0].size()) { if (board[nx][ny] != '#') { dfs(board, nx, ny, root,res); } } } board[x][y] = ch;
public List<String> findWords(char[][] board, String[] words) { Trietrie=newTrie(); for (String word : words) { trie.insert(word); }
Set<String> ans = newHashSet<String>(); for (inti=0; i < board.length; ++i) { for (intj=0; j < board[0].length; ++j) { dfs(board, trie, i, j, ans); } }
returnnewArrayList<String>(ans); }
publicvoiddfs(char[][] board, Trie now, int i1, int j1, Set<String> ans) { if (!now.children.containsKey(board[i1][j1])) { return; } charch= board[i1][j1]; Trienxt= now.children.get(ch); if (!"".equals(nxt.word)) { ans.add(nxt.word); nxt.word = ""; }
public IList<string> FindWords(char[][] board, string[] words) { Trie trie = new Trie(); foreach (string word in words) { trie.Insert(word); }
ISet<string> ans = new HashSet<string>(); for (int i = 0; i < board.Length; ++i) { for (int j = 0; j < board[0].Length; ++j) { DFS(board, trie, i, j, ans); } }
returnnew List<string>(ans); }
voidDFS(char[][] board, Trie now, int i1, int j1, ISet<string> ans) { if (!now.children.ContainsKey(board[i1][j1])) { return; } char ch = board[i1][j1]; Trie nxt = now.children[ch]; if (!"".Equals(nxt.word)) { ans.Add(nxt.word); nxt.word = ""; }
publicvoidInsert(string word) { Trie cur = this; foreach (char c in word) { if (!cur.children.ContainsKey(c)) { cur.children.Add(c, new Trie()); } cur = cur.children[c]; } cur.word = word; } }
type Trie struct { children map[byte]*Trie word string }
func(t *Trie) Insert(word string) { node := t for i := range word { ch := word[i] if node.children[ch] == nil { node.children[ch] = &Trie{children: map[byte]*Trie{}} } node = node.children[ch] } node.word = word }
var dirs = []struct{ x, y int }{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
funcfindWords(board [][]byte, words []string) (ans []string) { t := &Trie{children: map[byte]*Trie{}} for _, word := range words { t.Insert(word) }
m, n := len(board), len(board[0])
var dfs func(node *Trie, x, y int) dfs = func(node *Trie, x, y int) { ch := board[x][y] nxt := node.children[ch] if nxt == nil { return }
if nxt.word != "" { ans = append(ans, nxt.word) nxt.word = "" }
iflen(nxt.children) > 0 { board[x][y] = '#' for _, d := range dirs { nx, ny := x+d.x, y+d.y if0 <= nx && nx < m && 0 <= ny && ny < n && board[nx][ny] != '#' { dfs(nxt, nx, ny) } } board[x][y] = ch } iflen(nxt.children) == 0 { delete(node.children, ch) } } for i, row := range board { for j := range row { dfs(t, i, j) } }