当前位置:网站首页>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)
}
}
边栏推荐
- Navicat, MySQL export Er graph, er graph
- Go language - IO project
- Beego learning - JWT realizes user login and registration
- Beego learning - Tencent cloud upload pictures
- Explanation of the answers to the three questions
- Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
- The server denied password root remote connection access
- MySQL installation and configuration (command line version)
- 【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
- Temper cattle ranking problem
猜你喜欢
Construction of simple database learning environment
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
Flink学习笔记(九)状态编程
Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
Principles of computer composition - cache, connection mapping, learning experience
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
Redis learning (I)
Spark structured stream writing Hudi practice
LeetCode 535. Encryption and decryption of tinyurl
IDEA 中使用 Hudi
随机推荐
Flink学习笔记(八)多流转换
Apply for domain name binding IP to open port 80 record
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
2022-2-13 learning xiangniuke project - version control
Flink学习笔记(十一)Table API 和 SQL
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
MySQL installation and configuration (command line version)
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
LeetCode 30. Concatenate substrings of all words
Spark overview
Bert install no package metadata was found for the 'sacraments' distribution
[point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
Save the drama shortage, programmers' favorite high-score American drama TOP10
Go language - Reflection
Overview of image restoration methods -- paper notes
Tag paste operator (#)
Problems in the implementation of lenet
Integrated use of interlij idea and sonarqube
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature