当前位置:网站首页>0字典树/字符串中等 LeetCode676. 实现一个魔法字典
0字典树/字符串中等 LeetCode676. 实现一个魔法字典
2022-08-08 04:50:00 【18阿鲁】
分析
暴力的时间复杂度是
递归+前缀树
n*len(s)
class MagicDictionary {
Trie trie;
public MagicDictionary() {
trie = new Trie();
}
public void buildDict(String[] dictionary) {
for (String s : dictionary) {
Trie cur = trie;
for (char c : s.toCharArray()) {
int index = c - 'a';
if (cur.children[index] == null) {
cur.children[index] = new Trie();
}
cur = cur.children[index];
}
cur.end = true;
}
}
public boolean search(String searchWord) {
return dfs(searchWord,0,trie,1);
}
public boolean dfs (String word, int poi, Trie cur, int count) {
if (count < 0 || cur == null) {
return false;
}
if (poi == word.length()) {
return count == 0 && cur.end;
}
int c = word.charAt(poi) - 'a';
boolean ans = false;
for (int i = 0; i < 26; i++) {
if (i == c) {
ans |= dfs(word,poi+1,cur.children[i],count);
} else {
ans |= dfs(word,poi+1,cur.children[i],count-1);
}
if (ans) {
return true;
}
}
return false;
}
}
class Trie {
boolean end;
Trie[] children;
Trie() {
end = false;
children = new Trie[26];
}
}
/** * Your MagicDictionary object will be instantiated and called as such: * MagicDictionary obj = new MagicDictionary(); * obj.buildDict(dictionary); * boolean param_2 = obj.search(searchWord); */
边栏推荐
- 使用ffmpeg解码音频sdl(push)播放
- 【Win10】若干睡眠问题及对策
- The difference between classification, object detection, semantic segmentation, and instance segmentation
- 【js基础】闭包的几种情况(代码)
- 走进音视频的世界——RGB与YUV格式
- MySQL from entry to entry [20W word collection]
- The research project of the Institute of Metal Research of the Chinese Academy of Sciences has been certified by Huawei, helping to develop a new paradigm in materials science!
- 分类、目标检测、语义分割、实例分割的区别
- L3-006 迎风一刀斩
- MySQL中CHAR_LENGTH()和LENGTH()的区别
猜你喜欢

类似Bugfree的9大在线缺陷管理软件

MySQL4 (multi-table query)

Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk

【Win10】若干睡眠问题及对策

国内最主流的5大项目工时管理系统

二维码生成工具

【opencv】opencv开发包简介

一小时掌握vim基础用法

14.Unity2D 横版 粒子系统特效 飙血粒子+高处落地粒子+对象池管理所有粒子

Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model
随机推荐
The fledgling Xiao Li's 115th blog project notes on the creation of the domestic GD32F103RCT6 basic project
【直播回顾】昇思MindSpore易用性SIG2022上半年回顾总结
leetcode 112.路经总和 递归
强网杯 2019-随便注 (堆叠注入)
The research project of the Institute of Metal Research of the Chinese Academy of Sciences has been certified by Huawei, helping to develop a new paradigm in materials science!
Servlet---ServletConfig类使用介绍
The 5 most mainstream project time management systems in China
NetCore使用Dapper查询数据
QMI8658 - 6轴传感器学习笔记 - Ⅱ
【Redis】Redis学习——事务
Unity-CharacterController(角色控制器)
《动机与人格》笔记(二)——认识和理解的欲望
Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model
由联合体union引出的大小端问题
RecycleView配合Adapter调用notifyDataSetChanged闪屏?
分类、目标检测、语义分割、实例分割的区别
leetcode 70. Stair Climbing Dynamic Programming
MySQL从入门到入土【20W字收藏篇】
Lecture 84 Biweekly t4 6144 and Lecture 305 t4 6138
L3-005 Litter box distribution