【leetcode】1032. Stream of Characters
题目如下:
Implement the
StreamCheckerclass as follows:
StreamChecker(words): Constructor, init the data structure with the given words.query(letter): returns true if and only if for somek >= 1, the lastkcharacters 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 wordlistNote:
1 <= words.length <= 20001 <= 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的更多相关文章
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- 【LeetCode】157. Read N Characters Given Read4
Difficulty: Easy More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- 【LeetCode】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【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 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
随机推荐
- 170819-关于EL表达式的知识点
1 .EL表达式 [1] 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > ...
- DAO层单元测试编码和问题排查
DAO层单元测试编码和问题排查 SecKillDaoTest .java(注意接口参数使用注解@Parm(“parameter”)) package org.secKill.dao; import o ...
- jQuery file upload测试
<input id="fileupload" type="file" name="files[]" data-url="Ha ...
- ORA-01578: ORACLE 数据块损坏 (文件号 10, 块号 57896)ORA-01110: 数据文件 10: '/data/oradata/prod35.dbf'
https://community.oracle.com/thread/3540795 概述 ------------- 数据库坏块(corruption) 的类型可以按照坏块所属对象的不同,分为用户 ...
- mariadb(一)基础
一.数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来 ...
- VulnStack靶场实战(未完成)
环境搭建 https://www.cnblogs.com/HKCZ/p/11760213.html 信息收集 目录爆破 这里发现有phpmyadmin目录,这里可以直接获取webshell 参照: h ...
- Jquery的Ajax实现异步刷新
在Jquery中提供了一套ajax的方法,有: $.ajax([data],fn) load(url, [data], [callback]) $.get(url, [data], [callback ...
- SQL根据日期计算当月有多少天(转)
原文链接:https://blog.csdn.net/dobear_0922/article/details/2393235 --1.删除测试用表IF OBJECT_ID(N'Test', N'U') ...
- [Bzoj1014][JSOI2008]火星人prefix(无旋Treap&hash)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 因为涉及到增加和修改,所以后缀数组就被pass掉了,想到的就是平衡树维护hash值 ...
- kmp(前中后最长相同长度)
http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) ...