题目如下:

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 = xself.childDic = {}self.isword = Falseclass Trie(object):dic = {}def __init__(self):"""Initialize your data structure here."""self.root = TreeNode(None)self.dic = {}def insert(self,word):node = self.rootfor i in word:if i not in node.childDic:node.childDic[i] = TreeNode(i)node = node.childDic[i]node.isword = Truedef isExist(self,word):node = self.rootfor i in word:if i in node.childDic:node = node.childDic[i]if node.isword == True:return Trueelse:return Falseclass 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.qreturn self.t.isExist(self.q)

转载于:https://www.cnblogs.com/seyjs/p/10765511.html

【leetcode】1032. Stream of Characters相关推荐

  1. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  2. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  3. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  4. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  5. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  6. 【Leetcode】103. 二叉树的锯齿形层次遍历

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  7. 【Leetcode】79.单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...

  8. 【leetcode】 算法题1 两数之和

    [leetcode] 算法题1 两数之和 问题   给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums ...

  9. 【Leetcode】62. 不同路径

    题目 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为&qu ...

最新文章

  1. 大二暑假周进度报告之四
  2. 利用OpenCV的imread将RGB图像转化为灰度图像
  3. 渲染优化 lock unlock
  4. 10 邮件槽_员工主动发离职邮件,提出申请又反悔,法院判决让人懵了!
  5. mysql 中varchar_MYSQL中VARCHAR和CHAR类型
  6. 临床外显子组测序分析中的那些坑(上)
  7. ISO14443 PICC 与 PCD 调制解调方式
  8. Python中Base64编码与解码
  9. matlab chi2gof,chi2gof函数里的检验值P为什么总等于NaN呢
  10. 【机器学习】误差逆传播算法(反向传播算法)
  11. android11.0 Launcher3 高端定制之抽屉列表隐藏指定APP图标
  12. python常见语法错误
  13. CSS - (Cascading Style Sheets) 自学教程
  14. 故事系列之二:佛法世界中看天赋与勤奋
  15. html网页制作期末大作业成品_新疆旅游网页设计作品_dreamweaver作业静态HTML网页设计模板_新疆旅游景点网页作业制作...
  16. 【python】把Excel中的数据在页面中可视化
  17. 假设检验:使用p值来接受或拒绝你的假设
  18. 如何解决Mysql忘记用户名和密码
  19. (附源码)ssm电影院管理系统的设计与实现 毕业设计241505
  20. 【C语言】循环结构常见问题1

热门文章

  1. 决定局域网的主要技术要素
  2. gradle idea java ssm_应用框架:IDEA+Gradle创建MyBatis+SpringMVC项目
  3. java信息管理系统总结_java实现科研信息管理系统
  4. IPMSG飞鸽传书——编译源代码的方法
  5. Laravel提交POST请求报错
  6. [BJOI2015] 树的同构
  7. Oracle管理表空间和数据文件详解
  8. Python之路【第十四篇】:AngularJS --暂无内容-待更新
  9. 清楚xcode缓存(老是忘记所有记下来方便以后查阅)
  10. Python自然语言处理学习笔记(66):7.7 小结