当前位置:网站首页>【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 dictionarywords
Initialize object .f(string pref, string suff)
The return dictionary has a prefixprefix
And suffixessuff
The 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 <= 104
1 <= words[i].length <= 7
1 <= pref.length, suff.length <= 7
words[i]
、pref
andsuff
It's only made up of lowercase letters- Up to function
f
perform104
Secondary 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)
边栏推荐
- KubeCon + CloudNativeCon Europe 2022
- 容器适配器-栈,队列,优先级队列
- leetcode:78. subset
- Matlab绘制激活函数sigmoid,Relu
- 通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
- Binary search (leetcode704. very simple and necessary)
- How to read and write a single document based on MFC
- About 1000base-t1 1000Base-TX and 100base-t1
- Oracle queries the host name and the corresponding IP address
- options has an unknown property ‘before‘
猜你喜欢
[nodejs] npm/nrm cannot load the file because the script solution is prohibited in this system
【LeetCode】558. 四叉树交集
STM32 serial port sending and receiving multiple data tutorial based on gas sensor practice
支持工业级瘦设备4G接入,润和软件DAYU120通过OpenHarmony兼容性测评
laravel的问题
AI 之 OpenCvSharp 大圖找小圖(案例版)
一文搞懂│什么是跨域?如何解决跨域?
Bias and variance
GoogLeNet
options has an unknown property ‘before‘
随机推荐
2.9.2 digital type processing and convenient methods of ext JS
Dive Into Deep Learning——2.2数据预处理
Binary search (leetcode704. very simple and necessary)
【LeetCode】745. 前缀和后缀搜索
kubernetes学习之持久化存储StorageClass(4)
Chapter II: news topic classification tasks
Digital type processing and convenient method of ext JS
Chapter 4 用户数据分析
By voting for the destruction of STI by Dao, seektiger is truly community driven
Unity using Sqlite
Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)
MySQL master-slave setup
STM32串口发送和接收多个数据教程基于气体传感器实战
Analysis and processing of male and female voice signals based on MATLAB
数学建模比赛论文模板格式
About 1000base-t1 1000Base-TX and 100base-t1
S32k148evb about eNet loopback experiment
51单片机——双字节乘以双字节
Operator, assignment statement, structure description statement
laravel的问题