当前位置:网站首页>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)
}
}
边栏推荐
- With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
- CSDN markdown editor help document
- Crawler career from scratch (V): detailed explanation of re regular expression
- Spark 结构化流写入Hudi 实践
- Vscode编辑器右键没有Open In Default Browser选项
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- LeetCode每日一题(516. Longest Palindromic Subsequence)
- Jenkins learning (I) -- Jenkins installation
- NPM install installation dependency package error reporting solution
- Flink学习笔记(九)状态编程
猜你喜欢
2022-2-13 learning xiangniuke project - version control
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
Install third-party libraries such as Jieba under Anaconda pytorch
LeetCode 75. Color classification
【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
Build a solo blog from scratch
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
Alibaba cloud notes for the first time
Overview of image restoration methods -- paper notes
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
随机推荐
Trial of the combination of RDS and crawler
Beego learning - JWT realizes user login and registration
Idea uses the MVN command to package and report an error, which is not available
Hudi学习笔记(三) 核心概念剖析
LeetCode 30. Concatenate substrings of all words
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
Redis learning (I)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
2022-2-14 learning xiangniuke project - generate verification code
图像修复方法研究综述----论文笔记
Hudi 快速体验使用(含操作详细步骤及截图)
Install third-party libraries such as Jieba under Anaconda pytorch
Computing level network notes
Overview of database system
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
Using Hudi in idea
Hudi integrated spark data analysis example (including code flow and test results)
Apply for domain name binding IP to open port 80 record
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!