Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1048eef

Browse files
feat: add solutions to lc problems: No.3042,3045 (doocs#2350)
* No.3042.Count Prefix and Suffix Pairs I * No.3045.Count Prefix and Suffix Pairs II
1 parent 3c2e771 commit 1048eef

File tree

19 files changed

+892
-6
lines changed

19 files changed

+892
-6
lines changed

‎solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,194 @@ i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
6767
<!-- tabs:start -->
6868

6969
```python
70+
class Solution:
71+
def countPrefixSuffixPairs(self, words: List[str]) -> int:
72+
ans = 0
73+
for i, s in enumerate(words):
74+
for t in words[i + 1 :]:
75+
ans += t.endswith(s) and t.startswith(s)
76+
return ans
77+
```
78+
79+
```java
80+
class Solution {
81+
public int countPrefixSuffixPairs(String[] words) {
82+
int ans = 0;
83+
int n = words.length;
84+
for (int i = 0; i < n; ++i) {
85+
String s = words[i];
86+
for (int j = i + 1; j < n; ++j) {
87+
String t = words[j];
88+
if (t.startsWith(s) && t.endsWith(s)) {
89+
++ans;
90+
}
91+
}
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
int countPrefixSuffixPairs(vector<string>& words) {
102+
int ans = 0;
103+
int n = words.size();
104+
for (int i = 0; i < n; ++i) {
105+
string s = words[i];
106+
for (int j = i + 1; j < n; ++j) {
107+
string t = words[j];
108+
if (t.find(s) == 0 && t.rfind(s) == t.length() - s.length()) {
109+
++ans;
110+
}
111+
}
112+
}
113+
return ans;
114+
}
115+
};
116+
```
117+
118+
```go
119+
func countPrefixSuffixPairs(words []string) (ans int) {
120+
for i, s := range words {
121+
for _, t := range words[i+1:] {
122+
if strings.HasPrefix(t, s) && strings.HasSuffix(t, s) {
123+
ans++
124+
}
125+
}
126+
}
127+
return
128+
}
129+
```
130+
131+
```ts
132+
function countPrefixSuffixPairs(words: string[]): number {
133+
let ans = 0;
134+
for (let i = 0; i < words.length; ++i) {
135+
const s = words[i];
136+
for (const t of words.slice(i + 1)) {
137+
if (t.startsWith(s) && t.endsWith(s)) {
138+
++ans;
139+
}
140+
}
141+
}
142+
return ans;
143+
}
144+
```
145+
146+
<!-- tabs:end -->
147+
148+
### 方法二
149+
150+
<!-- tabs:start -->
70151

152+
```python
153+
class Node:
154+
__slots__ = ["children", "cnt"]
155+
156+
def __init__(self):
157+
self.children = {}
158+
self.cnt = 0
159+
160+
161+
class Solution:
162+
def countPrefixSuffixPairs(self, words: List[str]) -> int:
163+
ans = 0
164+
trie = Node()
165+
for s in words:
166+
node = trie
167+
for p in zip(s, reversed(s)):
168+
if p not in node.children:
169+
node.children[p] = Node()
170+
node = node.children[p]
171+
ans += node.cnt
172+
node.cnt += 1
173+
return ans
71174
```
72175

73176
```java
177+
class Node {
178+
Map<Integer, Node> children = new HashMap<>();
179+
int cnt;
180+
}
74181

182+
class Solution {
183+
public int countPrefixSuffixPairs(String[] words) {
184+
int ans = 0;
185+
Node trie = new Node();
186+
for (String s : words) {
187+
Node node = trie;
188+
int m = s.length();
189+
for (int i = 0; i < m; ++i) {
190+
int p = s.charAt(i) * 32 + s.charAt(m - i - 1);
191+
node.children.putIfAbsent(p, new Node());
192+
node = node.children.get(p);
193+
ans += node.cnt;
194+
}
195+
++node.cnt;
196+
}
197+
return ans;
198+
}
199+
}
75200
```
76201

77202
```cpp
203+
class Node {
204+
public:
205+
unordered_map<int, Node*> children;
206+
int cnt;
207+
208+
Node()
209+
: cnt(0) {}
210+
};
78211

212+
class Solution {
213+
public:
214+
int countPrefixSuffixPairs(vector<string>& words) {
215+
int ans = 0;
216+
Node* trie = new Node();
217+
for (const string& s : words) {
218+
Node* node = trie;
219+
int m = s.length();
220+
for (int i = 0; i < m; ++i) {
221+
int p = s[i] * 32 + s[m - i - 1];
222+
if (node->children.find(p) == node->children.end()) {
223+
node->children[p] = new Node();
224+
}
225+
node = node->children[p];
226+
ans += node->cnt;
227+
}
228+
++node->cnt;
229+
}
230+
return ans;
231+
}
232+
};
79233
```
80234
81235
```go
236+
type Node struct {
237+
children map[int]*Node
238+
cnt int
239+
}
82240
241+
func countPrefixSuffixPairs(words []string) (ans int) {
242+
trie := &Node{children: make(map[int]*Node)}
243+
for _, s := range words {
244+
node := trie
245+
m := len(s)
246+
for i := 0; i < m; i++ {
247+
p := int(s[i])*32 + int(s[m-i-1])
248+
if _, ok := node.children[p]; !ok {
249+
node.children[p] = &Node{children: make(map[int]*Node)}
250+
}
251+
node = node.children[p]
252+
ans += node.cnt
253+
}
254+
node.cnt++
255+
}
256+
return
257+
}
83258
```
84259

85260
<!-- tabs:end -->

‎solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,194 @@ Therefore, the answer is 0.</pre>
6363
<!-- tabs:start -->
6464

6565
```python
66+
class Solution:
67+
def countPrefixSuffixPairs(self, words: List[str]) -> int:
68+
ans = 0
69+
for i, s in enumerate(words):
70+
for t in words[i + 1 :]:
71+
ans += t.endswith(s) and t.startswith(s)
72+
return ans
73+
```
74+
75+
```java
76+
class Solution {
77+
public int countPrefixSuffixPairs(String[] words) {
78+
int ans = 0;
79+
int n = words.length;
80+
for (int i = 0; i < n; ++i) {
81+
String s = words[i];
82+
for (int j = i + 1; j < n; ++j) {
83+
String t = words[j];
84+
if (t.startsWith(s) && t.endsWith(s)) {
85+
++ans;
86+
}
87+
}
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int countPrefixSuffixPairs(vector<string>& words) {
98+
int ans = 0;
99+
int n = words.size();
100+
for (int i = 0; i < n; ++i) {
101+
string s = words[i];
102+
for (int j = i + 1; j < n; ++j) {
103+
string t = words[j];
104+
if (t.find(s) == 0 && t.rfind(s) == t.length() - s.length()) {
105+
++ans;
106+
}
107+
}
108+
}
109+
return ans;
110+
}
111+
};
112+
```
113+
114+
```go
115+
func countPrefixSuffixPairs(words []string) (ans int) {
116+
for i, s := range words {
117+
for _, t := range words[i+1:] {
118+
if strings.HasPrefix(t, s) && strings.HasSuffix(t, s) {
119+
ans++
120+
}
121+
}
122+
}
123+
return
124+
}
125+
```
126+
127+
```ts
128+
function countPrefixSuffixPairs(words: string[]): number {
129+
let ans = 0;
130+
for (let i = 0; i < words.length; ++i) {
131+
const s = words[i];
132+
for (const t of words.slice(i + 1)) {
133+
if (t.startsWith(s) && t.endsWith(s)) {
134+
++ans;
135+
}
136+
}
137+
}
138+
return ans;
139+
}
140+
```
141+
142+
<!-- tabs:end -->
143+
144+
### Solution 2
145+
146+
<!-- tabs:start -->
66147

148+
```python
149+
class Node:
150+
__slots__ = ["children", "cnt"]
151+
152+
def __init__(self):
153+
self.children = {}
154+
self.cnt = 0
155+
156+
157+
class Solution:
158+
def countPrefixSuffixPairs(self, words: List[str]) -> int:
159+
ans = 0
160+
trie = Node()
161+
for s in words:
162+
node = trie
163+
for p in zip(s, reversed(s)):
164+
if p not in node.children:
165+
node.children[p] = Node()
166+
node = node.children[p]
167+
ans += node.cnt
168+
node.cnt += 1
169+
return ans
67170
```
68171

69172
```java
173+
class Node {
174+
Map<Integer, Node> children = new HashMap<>();
175+
int cnt;
176+
}
70177

178+
class Solution {
179+
public int countPrefixSuffixPairs(String[] words) {
180+
int ans = 0;
181+
Node trie = new Node();
182+
for (String s : words) {
183+
Node node = trie;
184+
int m = s.length();
185+
for (int i = 0; i < m; ++i) {
186+
int p = s.charAt(i) * 32 + s.charAt(m - i - 1);
187+
node.children.putIfAbsent(p, new Node());
188+
node = node.children.get(p);
189+
ans += node.cnt;
190+
}
191+
++node.cnt;
192+
}
193+
return ans;
194+
}
195+
}
71196
```
72197

73198
```cpp
199+
class Node {
200+
public:
201+
unordered_map<int, Node*> children;
202+
int cnt;
203+
204+
Node()
205+
: cnt(0) {}
206+
};
74207

208+
class Solution {
209+
public:
210+
int countPrefixSuffixPairs(vector<string>& words) {
211+
int ans = 0;
212+
Node* trie = new Node();
213+
for (const string& s : words) {
214+
Node* node = trie;
215+
int m = s.length();
216+
for (int i = 0; i < m; ++i) {
217+
int p = s[i] * 32 + s[m - i - 1];
218+
if (node->children.find(p) == node->children.end()) {
219+
node->children[p] = new Node();
220+
}
221+
node = node->children[p];
222+
ans += node->cnt;
223+
}
224+
++node->cnt;
225+
}
226+
return ans;
227+
}
228+
};
75229
```
76230
77231
```go
232+
type Node struct {
233+
children map[int]*Node
234+
cnt int
235+
}
78236
237+
func countPrefixSuffixPairs(words []string) (ans int) {
238+
trie := &Node{children: make(map[int]*Node)}
239+
for _, s := range words {
240+
node := trie
241+
m := len(s)
242+
for i := 0; i < m; i++ {
243+
p := int(s[i])*32 + int(s[m-i-1])
244+
if _, ok := node.children[p]; !ok {
245+
node.children[p] = &Node{children: make(map[int]*Node)}
246+
}
247+
node = node.children[p]
248+
ans += node.cnt
249+
}
250+
node.cnt++
251+
}
252+
return
253+
}
79254
```
80255

81256
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /