【leetcode】1032. Stream of Characters

时间:2022-04-09 08:03:21

题目如下:

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&period; 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&period; Read N Characters Given Read4

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

  3. 【LeetCode】157&period; Read N Characters Given Read4 解题报告&lpar;C&plus;&plus;&rpar;

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

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

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

  5. 【leetcode】1002&period; Find Common Characters

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

  6. 【leetcode】557&period; 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&period; 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. wordpress(二)wordpress环境迁移

    迁移wordpress到服务器 本地环境如下 win8.1 appser 服务器环境如下 centos7 lnmp 1.使用phpmyadmin备份本地wordpress站点的数据库 2.备份本地wo ...

  2. angular的canvas画图例子

    angular的例子: <!DOCTYPE html> <html ng-app="APP"> <head> <meta charset= ...

  3. Maven 3&period;3&period;9在Windows上的安装

    开始学Maven了,可是我一个项目都木有做过.听过Maven 的大名,用来构建项目的. 下面记录下我安装Maven的过程 1.确认电脑上安装了JDK 在cmd下执行下列命令: java –versio ...

  4. C&num; JackLib系列之如何获取地球上两经纬度坐标点间的距离

    获取地球上两经纬度坐标点间的距离,利用[大圆距离公式]   A diagram illustrating great-circle distance (drawn in red) between tw ...

  5. Coursera-AndrewNg&lpar;吴恩达&rpar;机器学习笔记——第二周

    一.多变量线性回归问题(linear regression with multiple variables) 搭建环境OctaveWindows的安装包可由此链接获取:https://ftp.gnu. ...

  6. 杂谈---这些大忌,你在面试的时候发生过吗?(NO&period;1)

              面试是大部分人的人生当中难免会遇到的一件事,那么具体在面试当中有哪些忌讳呢? 说到面试,在这里尤其特指技术岗位的面试,很多时候,结果并不仅仅取决于你的技术广度与深度,亦或是你的笔试 ...

  7. js input监听兼容事件

    $('#phoneNumber').on('input',function() { var valueP = $(this).attr('value'); if(valueP.length == 11 ...

  8. linux 多线程之间信号传递

    函数 sigwait sigwait的含义就如同它的字面意思:等待某个信号的到来.如果调用该函数的线程没有等到它想等待的信号那么该线程就休眠.要达到等到一个信号,我们得做下面的事: 首先,定义一个信号 ...

  9. 软件工程-东北师大站-第六次作业PSP

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  10. JAVA Date超强工具类,可直接取代util&period;Date使用

    package net.maxt.util; import java.text.DateFormat; import java.text.ParseException; import java.tex ...