745. Prefix and Suffix Search Hard

@problem@discussion
#Hash Table#String#Design#Trie



1/**
2 * [745] Prefix and Suffix Search
3 *
4 * Design a special dictionary that searches the words in it by a prefix and a suffix.
5 * Implement the WordFilter class:
6 * 
7 * 	WordFilter(string[] words) Initializes the object with the words in the dictionary.
8 * 	f(string pref, string suff) Returns the index of the word in the dictionary, which has the prefix pref and the suffix suff. If there is more than one valid index, return the largest of them. If there is no such word in the dictionary, return -1.
9 * 
10 *  
11 * Example 1:
12 * 
13 * Input
14 * ["WordFilter", "f"]
15 * [[["apple"]], ["a", "e"]]
16 * Output
17 * [null, 0]
18 * Explanation
19 * WordFilter wordFilter = new WordFilter(["apple"]);
20 * wordFilter.f("a", "e"); // return 0, because the word at index 0 has prefix = "a" and suffix = "e".
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	1 <= words.length <= 10^4
26 * 	1 <= words[i].length <= 7
27 * 	1 <= pref.length, suff.length <= 7
28 * 	words[i], pref and suff consist of lowercase English letters only.
29 * 	At most 10^4 calls will be made to the function f.
30 * 
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/prefix-and-suffix-search/
35// discuss: https://leetcode.com/problems/prefix-and-suffix-search/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39struct WordFilter {
40        vec![]
41    }
42
43
44/** 
45 * `&self` means the method takes an immutable reference.
46 * If you need a mutable reference, change it to `&mut self` instead.
47 */
48impl WordFilter {
49
50    fn new(words: Vec<String>) -> Self {
51        
52    }
53    
54    fn f(&self, pref: String, suff: String) -> i32 {
55        
56    }
57}
58
59/**
60 * Your WordFilter object will be instantiated and called as such:
61 * let obj = WordFilter::new(words);
62 * let ret_1: i32 = obj.f(pref, suff);
63 */
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_745() {
73    }
74}
75


Back
© 2025 bowen.ge All Rights Reserved.