Skip to content

Commit 96fed65

Browse files
committed
0745 solved.
1 parent 642e5ac commit 96fed65

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0745)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0745 main.cpp)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/// Source : https://leetcode.com/problems/prefix-and-suffix-search/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-14
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
#include <map>
9+
10+
using namespace std;
11+
12+
13+
/// Suffix + Prefix
14+
/// Time Complexity: init: O(sum_of_characters)
15+
/// query: O(|prefix| + |suffix|)
16+
/// Space Complexity: O(sum_of_characters)
17+
class Trie {
18+
19+
private:
20+
struct Node{
21+
map<char, Node*> next;
22+
int index = -1;
23+
};
24+
Node* root;
25+
26+
public:
27+
Trie(){
28+
root = new Node();
29+
}
30+
31+
/** Inserts a word into the trie. */
32+
void insert(const string& word, int index){
33+
34+
Node* cur = root;
35+
bool flag = false;
36+
for(char c: word){
37+
if(cur->next.find(c) == cur->next.end()){
38+
cur->next[c] = new Node();
39+
}
40+
cur = cur->next[c];
41+
42+
if(c == '#') flag = true;
43+
if(flag) cur->index = index;
44+
}
45+
}
46+
47+
int get(const string& s) {
48+
49+
Node* cur = root;
50+
for(char c: s){
51+
if(cur->next.find(c) == cur->next.end())
52+
return -1;
53+
54+
cur = cur->next[c];
55+
}
56+
return cur->index;
57+
}
58+
};
59+
60+
class WordFilter {
61+
62+
private:
63+
Trie trie;
64+
65+
public:
66+
WordFilter(vector<string> words) {
67+
for(int i = 0 ; i < words.size() ; i ++){
68+
string s = words[i];
69+
for(int j = s.size() - 1; j >= 0; j --){
70+
string suf = s.substr(j);
71+
trie.insert(suf + "#" + s, i);
72+
}
73+
}
74+
75+
}
76+
77+
int f(string prefix, string suffix) {
78+
return trie.get(suffix + "#" + prefix);
79+
}
80+
};
81+
82+
83+
int main() {
84+
85+
return 0;
86+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
750750
| 740 | [Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/) | [solution](https://leetcode.com/problems/delete-and-earn/solution/) | [C++](0501-1000/0740-Delete-and-Earn/cpp-0740/) | | |
751751
| 741 | [Cherry Pickup](https://leetcode.com/problems/cherry-pickup/description/) | [solution](https://leetcode.com/problems/cherry-pickup/solution/)<br/>[缺:自底向上的动态规划] | [C++](0501-1000/0741-Cherry-Pickup/cpp-0741/) | | |
752752
| | | | | | |
753+
| 745 | [Prefix and Suffix Search](https://leetcode.com/problems/prefix-and-suffix-search/) | [solution](https://leetcode.com/problems/prefix-and-suffix-search/solution/) | [C++](0501-1000/0745-Prefix-and-Suffix-Search/cpp-0745/) | | |
753754
| 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [solution](https://leetcode.com/problems/min-cost-climbing-stairs/solution/) | [C++](0501-1000/0746-Min-Cost-Climbing-Stairs/cpp-0746/) | | |
754755
| 747 | [Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/) | [solution](https://leetcode.com/problems/largest-number-at-least-twice-of-others/solution/) | [C++](0501-1000/0747-Largest-Number-At-Least-Twice-of-Others/cpp-0747/) | | |
755756
| | | | | | |

0 commit comments

Comments
 (0)