[leetcode trie]212. Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

For example,
Given words = ["oath","pea","eat","rain"] and board =

[
  [‘o‘,‘a‘,‘a‘,‘n‘],
  [‘e‘,‘t‘,‘a‘,‘e‘],
  [‘i‘,‘h‘,‘k‘,‘r‘],
  [‘i‘,‘f‘,‘l‘,‘v‘]
]

Return ["eat","oath"].

题意:查找哪些单词在二维字母数组中出现

思路:1.用所有的单词建立字典树

   2.遍历二维数组中的所有位置,以这个位置开头向二维数组上下左右方向扩展的字符串是否在字典树中出现

  评论区还有一种用复数和字典树的解题方法,太帅了

 1 class Solution(object):
 2     def findWords(self, board, words):
 3         trie = {}
 4         for w in words:
 5             cur = trie
 6             for c in w:
 7                 cur = cur.setdefault(c,{})
 8             cur[None] = None
 9         self.flag = [[False]*len(board[0]) for _ in range(len(board))]
10         self.res = set()
11         for i in range(len(board)):
12             for j in range(len(board[0])):
13                 self.find(board,trie,i,j,‘‘)
14         return list(self.res)
15
16
17     def find(self,board,trie,i,j,str):
18         if None in trie:
19             self.res.add(str)
20         if i<0 or i>=len(board) or j<0 or j>=len(board[0]):
21             return
22         if not self.flag[i][j] and board[i][j] in trie:
23             self.flag[i][j] = True
24             self.find(board,trie[board[i][j]],i-1,j,str+board[i][j])
25             self.find(board,trie[board[i][j]],i+1,j,str+board[i][j])
26             self.find(board,trie[board[i][j]],i,j+1,str+board[i][j])
27             self.find(board,trie[board[i][j]],i,j-1,str+board[i][j])
28             self.flag[i][j] = False
29         return 
时间: 2024-12-15 01:42:38

[leetcode trie]212. Word Search II的相关文章

Java for LeetCode 212 Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

leetcode笔记:Word Search II

一. 题目描述 Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The

[leedcode 212] Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

[LeetCode#212]Word Search II

Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. Th

leetcode 212. Word Search II Add to List 查找单词---------- java

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

212. Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

79. Word Search/212. Word Search II--图的back tracking -- tier tree 待续

79题, 给你一个二维的board, 只能往上下左右四个方向走,为你是否能找到单词. board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] Given word = "ABCCED", return true. Given word = "SEE", return true. Given word = "ABCB", return false. 分析: 算法并不难,

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

[LeetCode] Word Search II

A simple combination of Implement Trie (Prefix Tree) and Word Search. If you've solved them, this problem will become easy :-) The following code is based on DFS and should be self-explanatory enough. Well, just go ahead and read it. It is long but c