当前位置:网站首页>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); */
边栏推荐
- MySQL从入门到入土【20W字收藏篇】
- 32. 你知道Redis的字符串是怎么实现的吗?
- How to play knowledge graph in recommender system
- 中间件的一些坑记录
- Spark entry learning-3-SparkSQL data abstraction
- 力扣84 双周赛 t4 6144 和力扣305周赛t4 6138
- 一小时掌握vim基础用法
- ES6解构赋值的使用说明
- 【冷启动】快手《POSO: Personalized Cold Start Modules for Large-scale Recommender Systems》
- sqlmap+dnslog注入复现
猜你喜欢

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

Redis设置开机自启动

NetCore uses Dapper to query data

TSF微服务治理实战系列(二)——服务路由

KMP和EXKMP(Z函数)

A line of code counts the number of occurrences of the specified string in the text

wpf中DataGrid的样式

C language - score and loop statement

This article will give you a thorough understanding of synchronized and Lock

2022-08-07 mysql/stonedb slow SQL-subquery-semi-join
随机推荐
Qt 日志模块的个性化使用
关于如何做选择
The difference between classification, object detection, semantic segmentation, and instance segmentation
Strong Net Cup 2019 - Casual Bet (Stacked Injection)
Young freshmen who yearn for open source | The guide to avoiding pits from open source to employment is here!
How to avoid bugs as much as possible
Let your text be seen by more people: Come and contribute, the payment is reliable!
Personalized use of Qt log module
Week 8 Transformer Language Models and Implications
一小时掌握vim基础用法
The difference between CHAR_LENGTH() and LENGTH() in MySQL
Machine Learning Notes: Learning Rate Warmup
单主机docker 搭建 redis-cluster
Research on Blind Recognition of Digital Modulated Signal Based on MindSpore Framework
【论文分享】异质图上的小样本学习:HG-Meta: Graph Meta-learning over Heterogeneous Graphs
基于MindSpore框架的数字调制信号盲识别研究
This article will give you a thorough understanding of synchronized and Lock
Go-Excelize API源码阅读(十)—— SetActiveSheet(index int)
torch.view() function usage
Lecture 84 Biweekly t4 6144 and Lecture 305 t4 6138