211. 添加与搜索单词 - 数据结构设计

设计一个支持以下两种操作的数据结构:

void addWord(word)
bool search(word)

search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。

示例:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

说明:

你可以假设所有单词都是由小写字母 a-z 组成的。

class WordDictionary {

    Map<Integer,Set<String>> map = new HashMap<>();//根据字符串长度分开存放
public WordDictionary() { }
public void addWord(String word) {
int length = word.length();
if(map.get(length)!=null){
map.get(length).add(word);
}else{
Set<String> set = new HashSet<>();
set.add(word);
map.put(length, set);
}
}
public boolean search(String word) {
Set<String> set = map.get(word.length());
if(set==null){ //不存在该长度的字符串,直接返回false;
return false;
}
if(set.contains(word)) return true;
char[] chars = word.toCharArray();
P:for(String s : set){
if(word.length()!=s.length()){
continue;
}
char[] cs = s.toCharArray();
for(int i = 0; i< cs.length; i++){//逐个字符对比
if(chars[i] != '.' && chars[i] != cs[i]){
continue P;
}
}
set.add(word);
return true;
}
return false;
}
} /**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/

Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计的更多相关文章

  1. leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  2. [LeetCode] 211. 添加与搜索单词 - 数据结构设计

    题目链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/ 题目描述: 设计一个支持以下两种操作的 ...

  3. Leetcode 211.添加与搜索单词

    添加与搜索单词 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字 ...

  4. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  5. 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

  6. [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  8. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. failed parsing overlays.

    clearn + rebuild + 重新运行: 删掉模拟器进程 + 重新运行:

  2. Pytest 单元测试框架

    1.pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效 2.安装 pytest pip install pytest 3.验证 pytest 是否安装成功 p ...

  3. python之邮件发送自动化

    # -*- coding:utf-8 -*-#@Time : 2020/3/24 22:55#@Autor: Mr.White#@File : 发送邮件.py 一.导入所需要的类 import smt ...

  4. Java中this()和super()

    Java关键字this只能用于方法体中.this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的th ...

  5. shell脚本命令 运行python文件&python命令行运行python代码

    单独的python文件运行的时候 报错: 在shell脚本中,运行shell脚本命令:在Python命令行中,运行Python代码.然而,“python hello.py”是一个脚本命令,不是pyth ...

  6. c#与js客户端之间相互传值

    RegisterStartupScript(key, script) RegisterClientScriptBlock(key, script) 第一个参数 key 是插入的客户端脚本的唯一标识符. ...

  7. SpringMVC底层执行原理

    一个简单的HelloSpringMVC程序 先在web,xml中注册一个前端控制器(DispatcherServlet) <?xml version="1.0" encodi ...

  8. PL/SQL语言语法

    一.前言 SQL全称是"结构化查询语言(Structured Query Language)",而PL/SQL是过程语言(Procedure Language),是对SQL的扩展. ...

  9. Spring注入的对象到底是什么类型

    开篇 之前,在用spring编码调试的时候,有时候发现被自动注入的对象是原始类的对象,有时候是代理类的对象,那什么时候注入的原始类对象呢,有什么时候注入的是代理类的对象呢?心里就留下了这个疑问.后来再 ...

  10. 当.Net成为大厂门槛代码小白该何去何从?

    掌握.Net已成为进入大厂的通行牌.越来越多的互联网软件公司开始使用.Net Core,根据去年数据显示腾讯.网易.顺丰.携程.中通.申通.同程艺龙.微医.233网校.问卷星.金蝶等关键业务已经在往. ...