当前位置:网站首页>【LeetCode】745. 前缀和后缀搜索
【LeetCode】745. 前缀和后缀搜索
2022-07-17 01:52:00 【pass night】
题目
设计一个包含一些单词的特殊词典,并能够通过前缀和后缀来检索单词。
实现 WordFilter 类:
WordFilter(string[] words)使用词典中的单词words初始化对象。f(string pref, string suff)返回词典中具有前缀prefix和后缀suff的单词的下标。如果存在不止一个满足要求的下标,返回其中 最大的下标 。如果不存在这样的单词,返回-1。
示例:
输入
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
输出
[null, 0]
解释
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // 返回 0 ,因为下标为 0 的单词:前缀 prefix = "a" 且 后缀 suff = "e" 。
提示:
1 <= words.length <= 1041 <= words[i].length <= 71 <= pref.length, suff.length <= 7words[i]、pref和suff仅由小写英文字母组成- 最多对函数
f执行104次调用
思路
- 将所有的前后缀组合保存在一个字典当中,访问则直接访问字典
代码
class WordFilter:
def __init__(self, words: List[str]):
self.dic = {
}
for i,word in enumerate(words):
for pre in range(1,len(word)+1):
for suff in range(1,len(word)+1):
self.dic[word[:pre] + "#" + word[-suff:]] = i
def f(self, pref: str, suff: str) -> int:
return self.dic.get(pref+"#"+suff,-1)
复杂度
- init
- 时间复杂度: O ( l e n g t h w o r d s ∗ l e n g t h w o r d 2 ) O(length_{words}*length_{word}^2) O(lengthwords∗lengthword2)
- 空间复杂度: O ( l e n g t h w o r d s ∗ l e n g t h w o r d 2 ) O(length_{words}*length_{word}^2) O(lengthwords∗lengthword2)
- f
- 时间复杂度: O ( 1 ) O(1) O(1)
- 空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
- 模块(block、module)的介绍
- oracle 查询 主机名和对应的IP地址
- XX市高中网络拓扑整体规划配置
- [2016 CCPC Hangzhou j] just a math problem (Mobius inversion)
- 箭头函数与this指向详解
- 洛谷每日三题之第三天(第四天补做)
- Properties of Gaussian distribution (including code)
- Basic IDL content of note 1: common data types_ Create array_ Type conversion_ Print output_ Basic operation_ Relational operation
- Mouse slide two pictures before and after comparison JS plug-in
- ClickHouse 中的公共表表达式 CTE
猜你喜欢

Es6 notes d'étude - station B Xiao Ma Ge

About 1000base-t1 1000Base-TX and 100base-t1

options has an unknown property ‘before‘

10. Redis interview FAQ

神经网络学习笔记2.2 ——用Matlab写一个简单的卷积神将网络图像分类器

SwiftUI 考试题库项目之支持题库和考试题库数量(教程含源码)

ResNet

Shell script receives and returns parameters
![Dqn theoretical basis and code implementation [pytoch + cartpole-v0]](/img/cf/32438e403544aa42e2fdd2e181327c.png)
Dqn theoretical basis and code implementation [pytoch + cartpole-v0]

上班摸鱼打卡模拟器微信小程序源码
随机推荐
Gdb+vscode for debugging 8 - use core to analyze dead cycles, deadlocks, and segment errors
基于Matlab的男女声音信号分析与处理
Oracle queries the maximum partition of non self growing partition
10. Redis interview FAQ
Matlab在线性代数中的应用
Installing PWA application in Google Chrome browser will display more description information
Frequency school and Bayes school
Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
Method of realizing horizontal and vertical centering of unknown width and height elements
MySQL 增删查改(基础)
Implementation of address book for dynamic memory management
[2016 CCPC Hangzhou j] just a math problem (Mobius inversion)
Zabbix6.0 monitoring vcenter7.0
Wdog and power mode of fs32k148 commissioning
MySQL addition, deletion, query and modification (basic)
Leetcode: 0-1 knapsack problem in dynamic programming [come and set the template directly]
谷歌 Chrome 浏览器安装 PWA 应用将显示更多描述信息
Dive into deep learning - 2.2 data preprocessing
Receiver operating curve
SparkCore核心设计:RDD,220716,