Skip to content

Commit c4eca26

Browse files
authored
feat: add solutions to lc problem: No.2744 (#1067)
No.2744.Find Maximum Number of String Pairs
1 parent e667710 commit c4eca26

File tree

7 files changed

+180
-6
lines changed

7 files changed

+180
-6
lines changed

solution/2700-2799/2744.Find Maximum Number of String Pairs/README.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,99 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68+
**方法一:哈希表**
69+
70+
我们可以用哈希表 $cnt$ 来存储数组 $words$ 中每个字符串的反转字符串出现的次数。
71+
72+
遍历数组 $words$,对于每个字符串 $w$,我们直接将 $cnt[w]$ 的值加到答案中,然后将 $w$ 的反转字符串出现的次数加 $1$。
73+
74+
遍历结束后,即可得到最大匹配数目。
75+
76+
时间复杂度 $O(L)$,空间复杂度 $O(L)$,其中 $L$ 是数组 $words$ 中字符串的长度之和。
77+
6878
<!-- tabs:start -->
6979

7080
### **Python3**
7181

7282
<!-- 这里可写当前语言的特殊实现逻辑 -->
7383

7484
```python
75-
85+
class Solution:
86+
def maximumNumberOfStringPairs(self, words: List[str]) -> int:
87+
cnt = Counter()
88+
ans = 0
89+
for w in words:
90+
ans += cnt[w]
91+
cnt[w[::-1]] += 1
92+
return ans
7693
```
7794

7895
### **Java**
7996

8097
<!-- 这里可写当前语言的特殊实现逻辑 -->
8198

8299
```java
83-
100+
class Solution {
101+
public int maximumNumberOfStringPairs(String[] words) {
102+
Map<String, Integer> cnt = new HashMap<>(words.length);
103+
int ans = 0;
104+
for (String w : words) {
105+
ans += cnt.getOrDefault(w, 0);
106+
cnt.merge(new StringBuilder(w).reverse().toString(), 1, Integer::sum);
107+
}
108+
return ans;
109+
}
110+
}
84111
```
85112

86113
### **C++**
87114

88115
```cpp
89-
116+
class Solution {
117+
public:
118+
int maximumNumberOfStringPairs(vector<string>& words) {
119+
unordered_map<string, int> cnt;
120+
int ans = 0;
121+
for (auto& w : words) {
122+
ans += cnt[w];
123+
reverse(w.begin(), w.end());
124+
cnt[w]++;
125+
}
126+
return ans;
127+
}
128+
};
90129
```
91130
92131
### **Go**
93132
94133
```go
134+
func maximumNumberOfStringPairs(words []string) (ans int) {
135+
cnt := map[string]int{}
136+
for _, w := range words {
137+
ans += cnt[w]
138+
s := []byte(w)
139+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
140+
s[i], s[j] = s[j], s[i]
141+
}
142+
cnt[string(s)]++
143+
}
144+
return
145+
}
146+
```
95147

148+
### **TypeScript**
149+
150+
```ts
151+
function maximumNumberOfStringPairs(words: string[]): number {
152+
const cnt: Map<string, number> = new Map();
153+
let ans = 0;
154+
for (const w of words) {
155+
ans += cnt.get(w) || 0;
156+
const s = w.split('').reverse().join('');
157+
cnt.set(s, (cnt.get(s) || 0) + 1);
158+
}
159+
return ans;
160+
}
96161
```
97162

98163
### **...**

solution/2700-2799/2744.Find Maximum Number of String Pairs/README_EN.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,80 @@ It can be proven that 1 is the maximum number of pairs that can be formed.
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def maximumNumberOfStringPairs(self, words: List[str]) -> int:
68+
cnt = Counter()
69+
ans = 0
70+
for w in words:
71+
ans += cnt[w]
72+
cnt[w[::-1]] += 1
73+
return ans
6774
```
6875

6976
### **Java**
7077

7178
```java
72-
79+
class Solution {
80+
public int maximumNumberOfStringPairs(String[] words) {
81+
Map<String, Integer> cnt = new HashMap<>(words.length);
82+
int ans = 0;
83+
for (String w : words) {
84+
ans += cnt.getOrDefault(w, 0);
85+
cnt.merge(new StringBuilder(w).reverse().toString(), 1, Integer::sum);
86+
}
87+
return ans;
88+
}
89+
}
7390
```
7491

7592
### **C++**
7693

7794
```cpp
78-
95+
class Solution {
96+
public:
97+
int maximumNumberOfStringPairs(vector<string>& words) {
98+
unordered_map<string, int> cnt;
99+
int ans = 0;
100+
for (auto& w : words) {
101+
ans += cnt[w];
102+
reverse(w.begin(), w.end());
103+
cnt[w]++;
104+
}
105+
return ans;
106+
}
107+
};
79108
```
80109
81110
### **Go**
82111
83112
```go
113+
func maximumNumberOfStringPairs(words []string) (ans int) {
114+
cnt := map[string]int{}
115+
for _, w := range words {
116+
ans += cnt[w]
117+
s := []byte(w)
118+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
119+
s[i], s[j] = s[j], s[i]
120+
}
121+
cnt[string(s)]++
122+
}
123+
return
124+
}
125+
```
84126

127+
### **TypeScript**
128+
129+
```ts
130+
function maximumNumberOfStringPairs(words: string[]): number {
131+
const cnt: Map<string, number> = new Map();
132+
let ans = 0;
133+
for (const w of words) {
134+
ans += cnt.get(w) || 0;
135+
const s = w.split('').reverse().join('');
136+
cnt.set(s, (cnt.get(s) || 0) + 1);
137+
}
138+
return ans;
139+
}
85140
```
86141

87142
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maximumNumberOfStringPairs(vector<string>& words) {
4+
unordered_map<string, int> cnt;
5+
int ans = 0;
6+
for (auto& w : words) {
7+
ans += cnt[w];
8+
reverse(w.begin(), w.end());
9+
cnt[w]++;
10+
}
11+
return ans;
12+
}
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func maximumNumberOfStringPairs(words []string) (ans int) {
2+
cnt := map[string]int{}
3+
for _, w := range words {
4+
ans += cnt[w]
5+
s := []byte(w)
6+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
7+
s[i], s[j] = s[j], s[i]
8+
}
9+
cnt[string(s)]++
10+
}
11+
return
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int maximumNumberOfStringPairs(String[] words) {
3+
Map<String, Integer> cnt = new HashMap<>(words.length);
4+
int ans = 0;
5+
for (String w : words) {
6+
ans += cnt.getOrDefault(w, 0);
7+
cnt.merge(new StringBuilder(w).reverse().toString(), 1, Integer::sum);
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maximumNumberOfStringPairs(self, words: List[str]) -> int:
3+
cnt = Counter()
4+
ans = 0
5+
for w in words:
6+
ans += cnt[w]
7+
cnt[w[::-1]] += 1
8+
return ans
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function maximumNumberOfStringPairs(words: string[]): number {
2+
const cnt: Map<string, number> = new Map();
3+
let ans = 0;
4+
for (const w of words) {
5+
ans += cnt.get(w) || 0;
6+
const s = w.split('').reverse().join('');
7+
cnt.set(s, (cnt.get(s) || 0) + 1);
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)