当前位置:网站首页>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)
}
}
边栏推荐
- Introduction to the basic application and skills of QT
- [point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
- 2022-2-14 learning xiangniuke project - generate verification code
- Redis learning (I)
- [kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
- Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
- We have a common name, XX Gong
- 2022-2-14 learning the imitation Niuke project - send email
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- Win10 quick screenshot
猜你喜欢

Spark 概述

Spark 结构化流写入Hudi 实践

Crawler career from scratch (IV): climb the bullet curtain of station B through API

【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks

What are the stages of traditional enterprise digital transformation?

Build a solo blog from scratch

Navicat, MySQL export Er graph, er graph

Temper cattle ranking problem

CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers

Jenkins learning (III) -- setting scheduled tasks
随机推荐
2022-2-13 learning the imitation Niuke project - home page of the development community
Database execution error: SQL_ mode only_ full_ group_ by:
Banner - Summary of closed group meeting
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
Spark overview
Solve POM in idea Comment top line problem in XML file
STM32F103 can learning record
Liteide is easy to use
LeetCode每日一题(2109. Adding Spaces to a String)
[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Modify idea code
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
Flink学习笔记(十一)Table API 和 SQL
Hudi 数据管理和存储概述
Integrated use of interlij idea and sonarqube
What are the stages of traditional enterprise digital transformation?
Digital statistics DP acwing 338 Counting problem
State compression DP acwing 291 Mondrian's dream
[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