当前位置:网站首页>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)
}
}
边栏推荐
- LeetCode每日一题(2109. Adding Spaces to a String)
- [kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
- Hudi 快速体验使用(含操作详细步骤及截图)
- Numerical analysis notes (I): equation root
- 数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
- LeetCode每日一题(1162. As Far from Land as Possible)
- Beego learning - JWT realizes user login and registration
- Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
- Derivation of Fourier transform
- LeetCode每日一题(968. Binary Tree Cameras)
猜你喜欢

Spark structured stream writing Hudi practice

【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字

Alibaba cloud notes for the first time

2022-2-14 learning the imitation Niuke project - send email

Apply for domain name binding IP to open port 80 record

Principles of computer composition - cache, connection mapping, learning experience

Spark overview

Jenkins learning (III) -- setting scheduled tasks

数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展

2022-2-13 learning the imitation Niuke project - home page of the development community
随机推荐
Win10安装ELK
LeetCode每日一题(516. Longest Palindromic Subsequence)
Learning C language from scratch -- installation and configuration of 01 MinGW
2022-2-14 learning the imitation Niuke project - send email
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
Hudi learning notes (III) analysis of core concepts
Database execution error: SQL_ mode only_ full_ group_ by:
IDEA 中使用 Hudi
About the configuration of vs2008+rade CATIA v5r22
Flink学习笔记(十一)Table API 和 SQL
用Redis实现分布式锁
Serializer rewrite: update and create methods
[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
Logstash+jdbc data synchronization +head display problems
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Jetson nano custom boot icon kernel logo CBOOT logo
【Kotlin学习】类、对象和接口——定义类继承结构
Win10 install elk
Install database -linux-5.7
Django operates Excel files through openpyxl to import data into the database in batches.