美文网首页
word-Search-II(路径查找)

word-Search-II(路径查找)

作者: 狼无雨雪 | 来源:发表于2019-07-18 14:46 被阅读0次

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

原先直接搜索和查找的方式到34个case的时候就超时了,一共偶36个case. 后面采用Trie(字典树的形式实现对前缀树的一次性查找避免多次查找耗费时间)

问题:

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.

示例1:

Input: 
board = [
  ['o','a','a','n'],
  ['e','t','a','e'],
  ['i','h','k','r'],
  ['i','f','l','v']
]
words = ["oath","pea","eat","rain"]

Output:
 ["eat","oath"]

示例2:

Input:
board = [["a","b"],["a","a"]]
words = ["aba","baa","bab","aaab","aaa","aaaa","aaba"]

Output:
['aaab', 'aaba', 'baa', 'aba', 'aaa']
import heapq
from typing import *
import functools
import copy
from collections import defaultdict

class Trie:
   def __init__(self):
       self.children = {}
       self.is_word = False
       self.word = None


class Solution: 
   def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
       
       
       length = len(board)
       size = len(board[0])
       
       if not board or not board[0] or not words:
           return []
       
       
       def makeTrie(words):
           trie = Trie()

           for word in words:
               point = trie
               for index, letter in enumerate(word):
                   
                   if not letter in point.children:
                       point.children[letter] = Trie()
                   
                   
                   
                   point = point.children[letter]
                   if index == len(word) - 1:
                       point.is_word = True
                       point.word = word
                   
           return trie
       root = makeTrie(words)
       result = set()
       
       def isOutRange(x,y):
           if x>=0 and x<length and y>=0 and y< size:
               return False
           else:
               return True
       
       
       def isTrue(i, j, final, visited, tree):
           
  
           
           if tree.is_word:
               final.add(tree.word)
           
           visited.add((i,j))
           
           
           
           nextVisited = [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]
           for n in nextVisited:
               if not n in visited and not isOutRange(n[0],n[1]) and board[n[0]][n[1]] in tree.children:
                   isTrue(n[0], n[1], final, visited, tree.children[board[n[0]][n[1]]])
           
           visited.remove((i,j))
           

       for i in range(length):
           for j in range(size):
               if board[i][j] in root.children:
                   isTrue(i,j,result,set(),root.children[board[i][j]])
       
       return list(result)

相关文章

  • word-Search-II(路径查找)

    https://leetcode.com/problems/word-search-ii/ 原先直接搜索和查找的方...

  • FIND实时查找

    文件查找find :实时查找工具,通过遍历指定路径完成文件查找 语法:find [OPTION]... [查找路径...

  • 路径查找

    int kern_path(const char *name, unsigned int flags, struc...

  • 常用命令记录

    查找命令路径

  • webpack笔记

    publicPath: 所有资源从这个路径开始查找resolve: 指定查找的路径path: webpack内置处...

  • 动态链接库问题_20170309

    动态链接库原理 用时查找 查找路径: 默认去/usr/lib /lib 下查找 可以设置自定义路径: export...

  • pip

    Python2 的路径查找命令:witch python Python3 的路径查找命令:which python...

  • 2022-01-11

    查找工程so库路径

  • AI知识点-布尔运算

    在窗口菜单中找到路径查找器,打开路径查找器面板; 1、联集,又称加法,将两个元素的路径合并在一起,形成一个路径; ...

  • UI学习笔记2 --- AI 基础

    1. AI 基础及路径查找器 PS 和 AI 的区别: AI 基本操作: 案例制作: 路径查找器: cmd+shi...

网友评论

      本文标题:word-Search-II(路径查找)

      本文链接:https://www.haomeiwen.com/subject/rfdllctx.html