Skip to content

Commit 037604b

Browse files
authored
Merge pull request #190 from LetMeFly666/2744
添加了问题“2744.最大字符串配对数目”的代码和题解
2 parents 1eea7a2 + 312c1e9 commit 037604b

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-01-17 22:37:26
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-01-17 22:47:56
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maximumNumberOfStringPairs(vector<string>& words) {
14+
unordered_map<string, int> ma;
15+
int ans = 0;
16+
for (string& word : words) {
17+
ans += ma[string(1, word[1]) + word[0]];
18+
ma[word]++;
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-01-17 22:48:44
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-01-17 22:48:50
6+
'''
7+
from typing import List
8+
from collections import defaultdict
9+
10+
class Solution:
11+
def maximumNumberOfStringPairs(self, words: List[str]) -> int:
12+
ma = defaultdict(int)
13+
ans = 0
14+
for word in words:
15+
ans += ma[word[1] + word[0]]
16+
ma[word] += 1
17+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@
518518
|2698.求一个整数的惩罚数|中等|<a href="https://leetcode.cn/problems/find-the-punishment-number-of-an-integer/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/25/LeetCode%202698.%E6%B1%82%E4%B8%80%E4%B8%AA%E6%95%B4%E6%95%B0%E7%9A%84%E6%83%A9%E7%BD%9A%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134043248" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-punishment-number-of-an-integer/solutions/2498448/letmefly-2698qiu-yi-ge-zheng-shu-de-chen-9h4c/" target="_blank">LeetCode题解</a>|
519519
|2706.购买两块巧克力|简单|<a href="https://leetcode.cn/problems/buy-two-chocolates/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/12/29/LeetCode%202706.%E8%B4%AD%E4%B9%B0%E4%B8%A4%E5%9D%97%E5%B7%A7%E5%85%8B%E5%8A%9B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135295305" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/buy-two-chocolates/solutions/2584324/letmefly-2706gou-mai-liang-kuai-qiao-ke-vw4ai/" target="_blank">LeetCode题解</a>|
520520
|2731.移动机器人|中等|<a href="https://leetcode.cn/problems/movement-of-robots/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/10/LeetCode%202731.%E7%A7%BB%E5%8A%A8%E6%9C%BA%E5%99%A8%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133758255" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/movement-of-robots/solutions/2476843/letmefly-2731yi-dong-ji-qi-ren-nao-jin-j-vwf2/" target="_blank">LeetCode题解</a>|
521+
|2744.最大字符串配对数目|简单|<a href="https://leetcode.cn/problems/find-maximum-number-of-string-pairs/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/17/LeetCode%202744.%E6%9C%80%E5%A4%A7%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%85%8D%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135662583" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-maximum-number-of-string-pairs/solutions/2608691/letmefly-2744zui-da-zi-fu-chuan-pei-dui-lnd3n/" target="_blank">LeetCode题解</a>|
521522
|2760.最长奇偶子数组|简单|<a href="https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/11/16/LeetCode%202760.%E6%9C%80%E9%95%BF%E5%A5%87%E5%81%B6%E5%AD%90%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134449952" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solutions/2529749/letmefly-2760zui-chang-qi-ou-zi-shu-zu-m-f5ob/" target="_blank">LeetCode题解</a>|
522523
|2807.在链表中插入最大公约数|中等|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/06/LeetCode%202807.%E5%9C%A8%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%8F%92%E5%85%A5%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135424056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/solutions/2593002/letmefly-2807zai-lian-biao-zhong-cha-ru-idjs4/" target="_blank">LeetCode题解</a>|
523524
|2824.统计和小于目标的下标对数目|简单|<a href="https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/11/24/LeetCode%202824.%E7%BB%9F%E8%AE%A1%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%9B%AE%E6%A0%87%E7%9A%84%E4%B8%8B%E6%A0%87%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134594377" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/solutions/2539873/letmefly-2824tong-ji-he-xiao-yu-mu-biao-7ulk5/" target="_blank">LeetCode题解</a>|
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: 2744.最大字符串配对数目
3+
date: 2024-01-17 22:37:15
4+
tags: [题解, LeetCode, 简单, 数组, 哈希表, 哈希, map, 字符串, 模拟]
5+
---
6+
7+
# 【LetMeFly】2744.最大字符串配对数目:哈希表
8+
9+
力扣题目链接:[https://leetcode.cn/problems/find-maximum-number-of-string-pairs/](https://leetcode.cn/problems/find-maximum-number-of-string-pairs/)
10+
11+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>words</code>&nbsp;,数组中包含 <strong>互不相同</strong>&nbsp;的字符串。</p>
12+
13+
<p>如果字符串&nbsp;<code>words[i]</code>&nbsp;与字符串 <code>words[j]</code>&nbsp;满足以下条件,我们称它们可以匹配:</p>
14+
15+
<ul>
16+
<li>字符串&nbsp;<code>words[i]</code>&nbsp;等于&nbsp;<code>words[j]</code>&nbsp;的反转字符串。</li>
17+
<li><code>0 &lt;= i &lt; j &lt; words.length</code></li>
18+
</ul>
19+
20+
<p>请你返回数组 <code>words</code>&nbsp;中的&nbsp;<strong>最大</strong>&nbsp;匹配数目。</p>
21+
22+
<p>注意,每个字符串最多匹配一次。</p>
23+
24+
<p>&nbsp;</p>
25+
26+
<p><strong>示例 1:</strong></p>
27+
28+
<pre>
29+
<b>输入:</b>words = ["cd","ac","dc","ca","zz"]
30+
<b>输出:</b>2
31+
<strong>解释:</strong>在此示例中,我们可以通过以下方式匹配 2 对字符串:
32+
- 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 "dc" 并且等于 words[2]。
33+
- 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 "ca" 并且等于 words[3]。
34+
可以证明最多匹配数目是 2 。
35+
</pre>
36+
37+
<p><strong>示例 2:</strong></p>
38+
39+
<pre>
40+
<b>输入:</b>words = ["ab","ba","cc"]
41+
<b>输出:</b>1
42+
<b>解释:</b>在此示例中,我们可以通过以下方式匹配 1 对字符串:
43+
- 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 "ab" 与 words[0] 相等。
44+
可以证明最多匹配数目是 1 。
45+
</pre>
46+
47+
<p><strong>示例 3:</strong></p>
48+
49+
<pre>
50+
<b>输入:</b>words = ["aa","ab"]
51+
<b>输出:</b>0
52+
<strong>解释:</strong>这个例子中,无法匹配任何字符串。
53+
</pre>
54+
55+
<p>&nbsp;</p>
56+
57+
<p><strong>提示:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= words.length &lt;= 50</code></li>
61+
<li><code>words[i].length == 2</code></li>
62+
<li><code>words</code>&nbsp;包含的字符串互不相同。</li>
63+
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>
64+
</ul>
65+
66+
67+
68+
## 方法一:哈希表
69+
70+
使用哈希表统计每个单词的出现次数。遍历单词数组,假设当前单词是word,将word反转后的字符串在哈希表中出现的次数累加到答案中。之后,将这个单词在哈希表中出现的次数加一。
71+
72+
+ 时间复杂度$O(len(words))$
73+
+ 空间复杂度$O(len(words))$
74+
75+
### AC代码
76+
77+
#### C++
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
int maximumNumberOfStringPairs(vector<string>& words) {
83+
unordered_map<string, int> ma;
84+
int ans = 0;
85+
for (string& word : words) {
86+
ans += ma[string(1, word[1]) + word[0]];
87+
ma[word]++;
88+
}
89+
return ans;
90+
}
91+
};
92+
```
93+
94+
#### Python
95+
96+
```python
97+
from typing import List
98+
from collections import defaultdict
99+
100+
class Solution:
101+
def maximumNumberOfStringPairs(self, words: List[str]) -> int:
102+
ma = defaultdict(int)
103+
ans = 0
104+
for word in words:
105+
ans += ma[word[1] + word[0]]
106+
ma[word] += 1
107+
return ans
108+
```
109+
110+
# 后记
111+
112+
What the Fantastic?
113+
114+
从昨天(2024.1.14)下午开始,本来很好用的ssh连接github的方式,突然一丁点都不能用了。。。不论是我实验室主机、实验室的另一台主机、阿里云服务器、实验室主机通过手机USB共享的流量(移动数据 和 校园网wifi)(都是来自北京的网),都:
115+
116+
```
117+
ssh: connect to host github.com port 22: Connection timed out
118+
fatal: Could not read from remote repository.
119+
120+
Please make sure you have the correct access rights
121+
and the repository exists.
122+
```
123+
124+
我写了个脚本不停地尝试重连,一次都没成功过。Github的ip地址被解析成了新加坡的一个IP,是微软的ip地址好像。
125+
126+
突然之间ssh连Github的方式也被完全地DNS污染了?之前还是10分钟能连上个4分钟,现在直接一下都不让访问了?
127+
128+
目前可用的方式:ssh的方式连ssh.github.com。别再疯了球球了。
129+
130+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/17/LeetCode%202744.%E6%9C%80%E5%A4%A7%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%85%8D%E5%AF%B9%E6%95%B0%E7%9B%AE/)哦~
131+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/135662583](https://letmefly.blog.csdn.net/article/details/135662583)

0 commit comments

Comments
 (0)