题目如下:

Implement the StreamChecker class as follows:

  • StreamChecker(words): Constructor, init the data structure with the given words.
  • query(letter): returns true if and only if for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.

Example:

StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary.
streamChecker.query('a'); // return false
streamChecker.query('b'); // return false
streamChecker.query('c'); // return false
streamChecker.query('d'); // return true, because 'cd' is in the wordlist
streamChecker.query('e'); // return false
streamChecker.query('f'); // return true, because 'f' is in the wordlist
streamChecker.query('g'); // return false
streamChecker.query('h'); // return false
streamChecker.query('i'); // return false
streamChecker.query('j'); // return false
streamChecker.query('k'); // return false
streamChecker.query('l'); // return true, because 'kl' is in the wordlist

Note:

  • 1 <= words.length <= 2000
  • 1 <= words[i].length <= 2000
  • Words will only consist of lowercase English letters.
  • Queries will only consist of lowercase English letters.
  • The number of queries is at most 40000.

解题思路:本题真对不起hard的级别,用字典树即可解决。首先在init的时候,把words中所有word逆置后存入字典树中;在query的时候,也有逆序的方式记录所有历史query过的值,同时判断其前缀是否存在于字典树中即可。

代码如下:

class TreeNode(object):
def __init__(self, x):
self.val = x
self.childDic = {}
self.isword = False class Trie(object):
dic = {}
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = TreeNode(None)
self.dic = {} def insert(self,word):
node = self.root
for i in word:
if i not in node.childDic:
node.childDic[i] = TreeNode(i)
node = node.childDic[i]
node.isword = True
def isExist(self,word):
node = self.root
for i in word:
if i in node.childDic:
node = node.childDic[i]
if node.isword == True:
return True
else:
return False class StreamChecker(object):
q = ''
def __init__(self, words):
"""
:type words: List[str]
"""
self.t = Trie()
for w in words:
self.t.insert(w[::-1]) def query(self, letter):
"""
:type letter: str
:rtype: bool
"""
#letter = letter[::-1]
self.q = letter + self.q
return self.t.isExist(self.q)

【leetcode】1032. Stream of Characters的更多相关文章

  1. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  2. 【LeetCode】157. Read N Characters Given Read4

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...

  3. 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

  4. 【LeetCode】1002. Find Common Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  5. 【leetcode】1002. Find Common Characters

    题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...

  6. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  7. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  8. 【leetcode】955. Delete Columns to Make Sorted II

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

  9. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

随机推荐

  1. OC端代码

    ViewController.m #import "ViewController.h"#import <Flutter/Flutter.h>#include " ...

  2. 如何把vue.js项目部署到服务器上

    如何把vue.js项目部署到服务器上面,我用的是tomcat服务器 1-改一下config/index.js文件,如下图,把assetsPublicPath: './', productionSour ...

  3. Javascript中this、prototype、constructor的理解(转载)

    http://www.cnblogs.com/phpmix/articles/1734031.html

  4. php面试专题---22、网站优化 总结

    php面试专题---22.网站优化 总结 一.总结 一句话总结: 主要从前端.后端.数据库.资源四个方面开始发散 前端浏览器缓存和数据压缩前端优化(减少HTTP请求次数) 资源流量优化(防盗链处理)C ...

  5. 测开之路八十五:python处理csv文件

    写入csv文件 一:写入字典 二:写入普通数据 读取: 第一种:普通读取 第二种:读取csv并用namedtuple映射列名,类似于使用类的实例 第三种:字典形式 import csvfrom col ...

  6. 为什么有mac地址还学要有IP地址??

    历史原因:早期的以太网只有集线器 ,没有交换机,所以发出去的包能被以太网内的所有机器监听到,因此要附带上MAC地址,每个机器只需要接受与自己MAC地址相匹配的包. 个人感觉上面的说法并不是太准确.找明 ...

  7. js 函数 写法

    // function ckeckName(){}; // function checkUser(){}; // function checkPassWorld(){}; // var checkNa ...

  8. HDU 5441 Travel (离线dsu)

    <题目链接> 题目大意:$n$个点,$m$条边,每条边具有对应的权值,然后进行$k$次询问,每次询问给定一个值,所有权值小于等于这个的边所对应的点能够相连,问每次询问,这些能够相互到达的点 ...

  9. JVM(14)之 类加载机制

    开发十年,就只剩下这套架构体系了! >>>   从本篇博文开始,我们就进入虚拟机类加载机制的学习了.那么什么是类加载呢?当我们写完一个Java类的时候,并不是直接就可以运行的,它还要 ...

  10. 【学习总结】GirlsInAI ML-diary day-21-初识 Numpy, Matplotlib, Seanborn [柱状图、折线图、箱图]

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day21 初识 Numpy, Matplotlib, Seanborn [柱状图.折线图.箱图] 一.Titanic练习赛 ...