当前位置:网站首页>【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)
边栏推荐
- Thinkphp5.0模型操作使用page进行分页
- 2022电工杯:5G 网络环境下应急物资配送问题(优化)
- 2022 electrician Cup: emergency material distribution in 5g network environment (optimization)
- 367. 有效的完全平方数(入门必会)
- 洛谷每日三题之第五天
- h5内嵌app,后和web如何进行通信?h5和web通信
- Fisher linear discriminant analysis
- SparkCore核心设计:RDD,220716,
- 一种鲁棒变形卷积神经网络图像去噪
- Operator, assignment statement, structure description statement
猜你喜欢

Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)

HRNet

Burpsuite2022.1详细安装步骤包含证书安装

leetcode162. Looking for peak

ulsm配置案例

Reptile learning (5): teach you reptile requests practice hand in hand

Through openharmony compatibility evaluation, the big brother development board and rich teaching and training resources have been ready

Thinkphp5.0 model operation uses page for paging

kubernetes学习之持久化存储StorageClass(4)

Chapter II: news topic classification tasks
随机推荐
options has an unknown property ‘before‘
Chapter II linear table
Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)
Receiver operating curve
374. 猜数字大小(入门 必会)
Oracle queries the host name and the corresponding IP address
Digital type processing and convenient method of ext JS
Reptile learning (5): teach you reptile requests practice hand in hand
Dive into deep learning - 2.2 data preprocessing
Neural network learning notes 2.2 -- write a simple convolution neural network image classifier with MATLAB
central limit theorem
S32k148evb about eNet loopback experiment
【Nodejs】npm/nrm无法加载文件、因为在此系统禁止执行脚本解决方式
h5内嵌app,后和web如何进行通信?h5和web通信
HRNet
XX City high school network topology overall planning configuration
Cmake common commands
51单片机——双字节乘以双字节
如何在自动化测试中使用MitmProxy获取数据返回?
Chapter II: news topic classification tasks