|
| 1 | +--- |
| 2 | +title: 3120.统计特殊字母的数量 I:(手写)哈希表 |
| 3 | +date: 2026-05-26 23:55:19 |
| 4 | +tags: [题解, LeetCode, 简单, 哈希表, set, 字符串] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】3120.统计特殊字母的数量 I:(手写)哈希表 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/count-the-number-of-special-characters-i/](https://leetcode.cn/problems/count-the-number-of-special-characters-i/) |
| 11 | + |
| 12 | +<p>给你一个字符串 <code>word</code>。如果 <code>word</code> 中同时存在某个字母的小写形式和大写形式,则称这个字母为 <strong>特殊字母</strong> 。</p> |
| 13 | + |
| 14 | +<p>返回 <code>word</code> 中<strong> </strong><strong>特殊字母 </strong>的数量。</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | + |
| 18 | +<p><strong class="example">示例 1:</strong></p> |
| 19 | + |
| 20 | +<div class="example-block"> |
| 21 | +<p><strong>输入:</strong><span class="example-io">word = "aaAbcBC"</span></p> |
| 22 | + |
| 23 | +<p><strong>输出:</strong><span class="example-io">3</span></p> |
| 24 | + |
| 25 | +<p><strong>解释:</strong></p> |
| 26 | + |
| 27 | +<p><code>word</code> 中的特殊字母是 <code>'a'</code>、<code>'b'</code> 和 <code>'c'</code>。</p> |
| 28 | +</div> |
| 29 | + |
| 30 | +<p><strong class="example">示例 2:</strong></p> |
| 31 | + |
| 32 | +<div class="example-block"> |
| 33 | +<p><strong>输入:</strong><span class="example-io">word = "abc"</span></p> |
| 34 | + |
| 35 | +<p><strong>输出:</strong><span class="example-io">0</span></p> |
| 36 | + |
| 37 | +<p><strong>解释:</strong></p> |
| 38 | + |
| 39 | +<p><code>word</code> 中不存在大小写形式同时出现的字母。</p> |
| 40 | +</div> |
| 41 | + |
| 42 | +<p><strong class="example">示例 3:</strong></p> |
| 43 | + |
| 44 | +<div class="example-block"> |
| 45 | +<p><strong>输入:</strong><span class="example-io">word = "abBCab"</span></p> |
| 46 | + |
| 47 | +<p><strong>输出:</strong>1</p> |
| 48 | + |
| 49 | +<p><strong>解释:</strong></p> |
| 50 | + |
| 51 | +<p><code>word</code> 中唯一的特殊字母是 <code>'b'</code>。</p> |
| 52 | +</div> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | + |
| 56 | +<p><strong>提示:</strong></p> |
| 57 | + |
| 58 | +<ul> |
| 59 | + <li><code>1 <= word.length <= 50</code></li> |
| 60 | + <li><code>word</code> 仅由小写和大写英文字母组成。</li> |
| 61 | +</ul> |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +## 解题方法:哈希表 |
| 66 | + |
| 67 | +遍历一般字符串并把所有出现过的字符放入哈希表中,从0到25遍历这26个字母,看哪个字母的大小写都出现过。 |
| 68 | + |
| 69 | +有办法使用数组代替(作为)哈希表吗?以及有办法使用52长度的数组作为哈希表吗?有,判断下大小写放入数组对应的位置就好。 |
| 70 | + |
| 71 | ++ 时间复杂度$O(len(word)+C)$,其中$C=26\times 2$ |
| 72 | ++ 空间复杂度$O(C)$ |
| 73 | + |
| 74 | +### AC代码 |
| 75 | + |
| 76 | +#### C++ |
| 77 | + |
| 78 | +```cpp |
| 79 | +/* |
| 80 | + * @LastEditTime: 2026-05-26 23:53:14 |
| 81 | + */ |
| 82 | +class Solution { |
| 83 | +public: |
| 84 | + int numberOfSpecialChars(string word) { |
| 85 | + bool lower[26] = {false}, upper[26] = {false}; |
| 86 | + for (char c : word) { |
| 87 | + if ('a' <= c && c <= 'z') { |
| 88 | + lower[c - 'a'] = true; |
| 89 | + } else { |
| 90 | + upper[c - 'A'] = true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + int ans = 0; |
| 95 | + for (int i = 0; i < 26; i++) { |
| 96 | + ans += lower[i] && upper[i]; |
| 97 | + } |
| 98 | + return ans; |
| 99 | + } |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/161432822)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/05/26/LeetCode%203120.%E7%BB%9F%E8%AE%A1%E7%89%B9%E6%AE%8A%E5%AD%97%E6%AF%8D%E7%9A%84%E6%95%B0%E9%87%8FI/)哦~ |
| 104 | +> |
| 105 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments