当前位置:网站首页>Leetcode daily question (745. prefix and suffix search)
Leetcode daily question (745. prefix and suffix search)
2022-07-03 09:33: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.
Treat each word alphabetically into suffix#prefix Combination form , Then you can query , The time complexity of query is O(1), What we should pay attention to here is , Every suffix#prefix Of key Only need to correspond to one of the largest index Just fine , Because that's what the query requires
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)
}
}
边栏推荐
- There is no open in default browser option in the right click of the vscade editor
- Utilisation de hudi dans idea
- Numerical analysis notes (I): equation root
- Move anaconda, pycharm and jupyter notebook to mobile hard disk
- Beego learning - JWT realizes user login and registration
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
- Hudi 快速体验使用(含操作详细步骤及截图)
- Serializer rewrite: update and create methods
- 【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
- 制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
猜你喜欢
Flink学习笔记(八)多流转换
Idea uses the MVN command to package and report an error, which is not available
Modify idea code
Win10 install elk
Construction of simple database learning environment
There is no open in default browser option in the right click of the vscade editor
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
图像修复方法研究综述----论文笔记
Learning C language from scratch -- installation and configuration of 01 MinGW
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
随机推荐
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
Send mail using WP mail SMTP plug-in
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Flink学习笔记(十)Flink容错机制
Hudi data management and storage overview
[untitled] use of cmake
Spark cluster installation and deployment
Please tell me how to set vscode
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
Using Hudi in idea
Common formulas of probability theory
Hudi学习笔记(三) 核心概念剖析
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Install database -linux-5.7
Jestson Nano 从tftp服务器下载更新kernel和dtb
Esp32 at command does not respond
Desktop icon recognition based on OpenCV
LeetCode每日一题(745. Prefix and Suffix Search)
Logstash+jdbc data synchronization +head display problems