|
| 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> 开始的数组 <code>words</code> ,数组中包含 <strong>互不相同</strong> 的字符串。</p> |
| 12 | + |
| 13 | +<p>如果字符串 <code>words[i]</code> 与字符串 <code>words[j]</code> 满足以下条件,我们称它们可以匹配:</p> |
| 14 | + |
| 15 | +<ul> |
| 16 | + <li>字符串 <code>words[i]</code> 等于 <code>words[j]</code> 的反转字符串。</li> |
| 17 | + <li><code>0 <= i < j < words.length</code></li> |
| 18 | +</ul> |
| 19 | + |
| 20 | +<p>请你返回数组 <code>words</code> 中的 <strong>最大</strong> 匹配数目。</p> |
| 21 | + |
| 22 | +<p>注意,每个字符串最多匹配一次。</p> |
| 23 | + |
| 24 | +<p> </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> </p> |
| 56 | + |
| 57 | +<p><strong>提示:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>1 <= words.length <= 50</code></li> |
| 61 | + <li><code>words[i].length == 2</code></li> |
| 62 | + <li><code>words</code> 包含的字符串互不相同。</li> |
| 63 | + <li><code>words[i]</code> 只包含小写英文字母。</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