当前位置:网站首页>【LeetCode】745. Prefix and suffix search
【LeetCode】745. Prefix and suffix search
2022-07-19 03:45:00 【pass night】
subject
Design a special dictionary that contains some words , And can retrieve words through prefixes and suffixes .
Realization WordFilter class :
WordFilter(string[] words)Use words in the dictionarywordsInitialize object .f(string pref, string suff)The return dictionary has a prefixprefixAnd suffixessuffThe subscript of the word . If there is more than one subscript that meets the requirements , Return to it The largest subscript . If there is no such word , return-1.
Example :
Input
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
Output
[null, 0]
explain
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // return 0 , Because the index is zero 0 's words : Prefix prefix = "a" And suffix suff = "e" .
Tips :
1 <= words.length <= 1041 <= words[i].length <= 71 <= pref.length, suff.length <= 7words[i]、prefandsuffIt's only made up of lowercase letters- Up to function
fperform104Secondary call
Ideas
- Save all pre suffix combinations in a dictionary , Access directly to the dictionary
Code
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)
Complexity
- init
- Time complexity : 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)
- Spatial complexity : 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
- Time complexity : O ( 1 ) O(1) O(1)
- Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- 洛谷每日三题之第五天
- Neural network learning notes 2.2 -- write a simple convolution neural network image classifier with MATLAB
- The fourth day of the third question of daily Luogu
- Unity解决同材质物体重叠产生Z-Fighting的问题
- The third day of the three questions of Luogu daily (make up on the fourth day)
- 【LeetCode】558. 四叉树交集
- VGG (Visual Geometry Group)
- Introduction of modules (block, module)
- 本地存储localStorage⽤法详解
- 通过OpenHarmony兼容性测评,大师兄开发板与丰富教培资源已ready
猜你喜欢

web语义化(强调标签-em-斜体)(重点强调标签-strong-粗体)(自定义列表:dl、dt、dd)

2022 electrician Cup: emergency material distribution in 5g network environment (optimization)

GoogLeNet

洛谷每日三题之第三天(第四天补做)

Gnome boxes virtual machine creation and installation

一文搞懂│什么是跨域?如何解决跨域?

374. 猜数字大小(入门 必会)

时间轴组件

通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动

基于Pandoc与VSCode的 LaTeX环境配置
随机推荐
Work fishing clock simulator wechat applet source code
Introduction of modules (block, module)
[2016 CCPC Hangzhou j] just a math problem (Mobius inversion)
箭头函数与this指向详解
Local storage localstorage ⽤ method details
Number of supported question banks and examination question banks of swiftui examination question bank project (tutorial includes source code)
Ouvrir le cvsharp d'ai pour trouver une petite image (version de cas)
Oracle queries the maximum partition of non self growing partition
oracle 查询 主机名和对应的IP地址
Installing PWA application in Google Chrome browser will display more description information
模块(block、module)的介绍
Bias and variance
Unity using Sqlite
sublime基本操作
oracle 查询非自增长分区的最大分区
STM32 serial port sending and receiving multiple data tutorial based on gas sensor practice
神器网站目录,全都是刚需好用的网站
Fisher linear discriminant analysis
kubernetes学习之持久化存储StorageClass(4)
central limit theorem