当前位置:网站首页>LeetCode每日一题(745. Prefix and Suffix Search)
LeetCode每日一题(745. Prefix and Suffix Search)
2022-07-03 09:01:00 【wangjun861205】
Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.
Implement the WordFilter class:
WordFilter(string[] words) Initializes the object with the words in the dictionary.
f(string prefix, string suffix) Returns the index of the word in the dictionary, which has the prefix prefix and the suffix suffix. If there is more than one valid index, return the largest of them. If there is no such word in the dictionary, return -1.
Example 1:
Input
[“WordFilter”, “f”] > [[[“apple”]], [“a”, “e”]]
Output
[null, 0]
Explanation
WordFilter wordFilter = new WordFilter([“apple”]);
wordFilter.f(“a”, “e”); // return 0, because the word at index 0 has prefix = “a” and suffix = 'e".
Constraints:
- 1 <= words.length <= 15000
- 1 <= words[i].length <= 10
- 1 <= prefix.length, suffix.length <= 10
- words[i], prefix and suffix consist of lower-case English letters only.
- At most 15000 calls will be made to the function f.
把每个单词按字母处理成 suffix#prefix 组合形式, 然后就可以进行查询了, 查询时时间复杂度为 O(1), 这里要注意的是,每个 suffix#prefix 的 key 仅需要对应一个最大的 index 就好了, 因为查询就是这么要求的
use std::collections::HashMap;
struct WordFilter {
m: HashMap<String, i32>,
}
/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */
impl WordFilter {
fn new(words: Vec<String>) -> Self {
let mut m = HashMap::new();
for (i, word) in words.into_iter().enumerate() {
let mut prefix = String::new();
for pc in word.chars() {
prefix.push(pc);
let mut suffix = String::new();
for sc in word.chars().rev() {
suffix.insert(0, sc);
*m.entry(suffix.clone() + "#" + &prefix).or_insert(0) = i as i32;
}
}
}
Self {
m }
}
fn f(&self, prefix: String, suffix: String) -> i32 {
*self.m.get(&(suffix + "#" + &prefix)).unwrap_or(&-1)
}
}
边栏推荐
- LeetCode 75. Color classification
- Save the drama shortage, programmers' favorite high-score American drama TOP10
- 【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
- 【点云处理之论文狂读经典版12】—— FoldingNet: Point Cloud Auto-encoder via Deep Grid Deformation
- Digital statistics DP acwing 338 Counting problem
- Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
- CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
- Flink-CDC实践(含实操步骤与截图)
- The idea of compiling VBA Encyclopedia
- ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
猜你喜欢

Hudi learning notes (III) analysis of core concepts
![[kotlin learning] classes, objects and interfaces - define class inheritance structure](/img/66/34396e51c59504ebbc6b6eb9831209.png)
[kotlin learning] classes, objects and interfaces - define class inheritance structure

Move anaconda, pycharm and jupyter notebook to mobile hard disk

Spark cluster installation and deployment

【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数

Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)

Recommend a low code open source project of yyds
![[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)](/img/fd/c0f885cdd17f1d13fdbc71b2aea641.jpg)
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)

Introduction to the basic application and skills of QT

Run flash demo on ECS
随机推荐
Integrated use of interlij idea and sonarqube
Beego learning - JWT realizes user login and registration
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
Hudi学习笔记(三) 核心概念剖析
Spark structured stream writing Hudi practice
Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
NPM install installation dependency package error reporting solution
[advanced feature learning on point clouds using multi resolution features and learning]
Flink学习笔记(十一)Table API 和 SQL
Powerdesign reverse wizard such as SQL and generates name and comment
Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
Navicat, MySQL export Er graph, er graph
LeetCode 75. Color classification
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Jenkins learning (I) -- Jenkins installation
Flink学习笔记(八)多流转换
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
2022-2-14 learning xiangniuke project - generate verification code
2022-2-13 learning xiangniuke project - version control
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature