Skip to content

Commit 9ca6097

Browse files
committed
update: 添加问题“3120.统计特殊字母的数量I”的代码和题解 (#1607)
3120: AC.cpp (#1606) - AC,22.22%,57.14% + (en) Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 5fb5f9e commit 9ca6097

6 files changed

Lines changed: 145 additions & 24 deletions

File tree

.commitmsg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3120: AC.cpp (#1606) - AC,22.22%,57.14% + (en)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-05-26 23:50:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-05-26 23:53:14
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int numberOfSpecialChars(string word) {
14+
bool lower[26] = {false}, upper[26] = {false};
15+
for (char c : word) {
16+
if ('a' <= c && c <= 'z') {
17+
lower[c - 'a'] = true;
18+
} else {
19+
upper[c - 'A'] = true;
20+
}
21+
}
22+
23+
int ans = 0;
24+
for (int i = 0; i < 26; i++) {
25+
ans += lower[i] && upper[i];
26+
}
27+
return ans;
28+
}
29+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@
10571057
|3110.字符串的分数|简单|<a href="https://leetcode.cn/problems/score-of-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/15/LeetCode%203110.%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%86%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146275888" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/score-of-a-string/solutions/3612406/letmefly-3110zi-fu-chuan-de-fen-shu-mo-n-aqpo/" target="_blank">LeetCode题解</a>|
10581058
|3112.访问消失节点的最少时间|中等|<a href="https://leetcode.cn/problems/minimum-time-to-visit-disappearing-nodes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/18/LeetCode%203112.%E8%AE%BF%E9%97%AE%E6%B6%88%E5%A4%B1%E8%8A%82%E7%82%B9%E7%9A%84%E6%9C%80%E5%B0%91%E6%97%B6%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140530368" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-time-to-visit-disappearing-nodes/solutions/2848666/letmefly-3112fang-wen-xiao-shi-jie-dian-1cgwc/" target="_blank">LeetCode题解</a>|
10591059
|3115.质数的最大距离|中等|<a href="https://leetcode.cn/problems/maximum-prime-difference/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/02/LeetCode%203115.%E8%B4%A8%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E8%B7%9D%E7%A6%BB/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140121329" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-prime-difference/solutions/2828631/letmefly-3115zhi-shu-de-zui-da-ju-chi-zh-1yvd/" target="_blank">LeetCode题解</a>|
1060+
|3120.统计特殊字母的数量I|简单|<a href="https://leetcode.cn/problems/count-the-number-of-special-characters-i/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/161432822" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-special-characters-i/solutions/3974744/letmefly-3120tong-ji-te-shu-zi-mu-de-shu-bata/" target="_blank">LeetCode题解</a>|
10601061
|3127.构造相同颜色的正方形|简单|<a href="https://leetcode.cn/problems/make-a-square-with-the-same-color/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/31/LeetCode%203127.%E6%9E%84%E9%80%A0%E7%9B%B8%E5%90%8C%E9%A2%9C%E8%89%B2%E7%9A%84%E6%AD%A3%E6%96%B9%E5%BD%A2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141756605" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/make-a-square-with-the-same-color/solutions/2899199/letmefly-3127gou-zao-xiang-tong-yan-se-d-xp4l/" target="_blank">LeetCode题解</a>|
10611062
|3131.找出与数组相加的整数I|简单|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/08/LeetCode%203131.%E6%89%BE%E5%87%BA%E4%B8%8E%E6%95%B0%E7%BB%84%E7%9B%B8%E5%8A%A0%E7%9A%84%E6%95%B4%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141037760" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-i/solutions/2873708/letmefly-3131zhao-chu-yu-shu-zu-xiang-ji-mfd3/" target="_blank">LeetCode题解</a>|
10621063
|3132.找出与数组相加的整数II|中等|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/09/LeetCode%203132.%E6%89%BE%E5%87%BA%E4%B8%8E%E6%95%B0%E7%BB%84%E7%9B%B8%E5%8A%A0%E7%9A%84%E6%95%B4%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141072842" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-integer-added-to-array-ii/solutions/2874942/letmefly-3132zhao-chu-yu-shu-zu-xiang-ji-eljs/" target="_blank">LeetCode题解</a>|
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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>&nbsp;</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>&nbsp;</p>
55+
56+
<p><strong>提示:</strong></p>
57+
58+
<ul>
59+
<li><code>1 &lt;= word.length &lt;= 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)

test.cpp

Lines changed: 0 additions & 24 deletions
This file was deleted.

toSay.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!--
2+
* @Author: LetMeFly
3+
* @Date: 2026-05-26 23:54:27
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-05-26 23:54:34
6+
-->
7+
todo: merge confilct
8+
9+
今日扇贝单词web端改版了

0 commit comments

Comments
 (0)