当前位置:网站首页>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)
}
}
边栏推荐
- Internet Protocol learning record
- About the configuration of vs2008+rade CATIA v5r22
- 2022-2-13 learning the imitation Niuke project - home page of the development community
- Serializer rewrite: update and create methods
- [point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
- Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
- CATIA automation object architecture - detailed explanation of application objects (III) systemservice
- 2022-2-13 learning xiangniuke project - version control
- 2022-2-14 learning xiangniuke project - Session Management
- LeetCode 75. Color classification
猜你喜欢

State compression DP acwing 91 Shortest Hamilton path

Build a solo blog from scratch
![[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling](/img/40/e0c7bad60b19cafa467c229419ac21.png)
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling

Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation

Construction of simple database learning environment

Windows安装Redis详细步骤

【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature

Solve POM in idea Comment top line problem in XML file
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years

Run flash demo on ECS
随机推荐
Uc/os self-study from 0
Spark 概述
【点云处理之论文狂读前沿版8】—— Pointview-GCN: 3D Shape Classification With Multi-View Point Clouds
Serializer rewrite: update and create methods
Tag paste operator (#)
State compression DP acwing 291 Mondrian's dream
Spark overview
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
Temper cattle ranking problem
Data mining 2021-4-27 class notes
LeetCode 535. Encryption and decryption of tinyurl
【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
On February 14, 2022, learn the imitation Niuke project - develop the registration function
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
Construction of simple database learning environment
C language programming specification
【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
[point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals