leetcode 3120. 统计特殊字母的数量 I 简单

给你一个字符串 word。如果 word 中同时存在某个字母的小写形式和大写形式,则称这个字母为 特殊字母 。

返回 word 中特殊字母的数量。

示例 1:

**输入:**word = "aaAbcBC"

**输出:**3

解释:

word 中的特殊字母是 'a'、'b' 和 'c'。

示例 2:

**输入:**word = "abc"

**输出:**0

解释:

word 中不存在大小写形式同时出现的字母。

示例 3:

**输入:**word = "abBCab"

**输出:**1

解释:

word 中唯一的特殊字母是 'b'。

提示:

  • 1 <= word.length <= 50
  • word 仅由小写和大写英文字母组成。

分析:用两个哈希数组分别记录大写字母和小写字母是否出现过,接着检查所有的字母,如果大小写都出现过,则是特殊字母,计数 +1,最后返回特殊字母数量。

cpp 复制代码
class Solution {
public:
    int numberOfSpecialChars(string word) {
        int ans=0,lowwer[30]={0},upper[30]={0};
        for(int i=0;word[i];++i)
        {
            if(word[i]>='A'&&word[i]<='Z')upper[word[i]-'A']++;
            else lowwer[word[i]-'a']++;
        }
        for(int i=0;i<26;++i)
            if(lowwer[i]&&upper[i])ans++;
        return ans;
    }
};
相关推荐
Nil2081 天前
leetcode 49字母异位分组
算法·leetcode·哈希算法
Nil2082 天前
leetcode 5最长回文子串
算法·leetcode·职场和发展
m0_674571972 天前
力扣 hot 100 python
python·算法·leetcode
圣保罗的大教堂2 天前
leetcode 3727. 最大交替平方和 中等
leetcode
Xin7702 天前
LeetCode 200. 岛屿数量(DFS 深度优先遍历法)
leetcode
alphaTao2 天前
LeetCode 每日一题 2026/10/12-2026/10/18
算法·leetcode
Cccp.1232 天前
【leetcode】(十三)大数据处理
数据结构·算法·leetcode
Pniubi2 天前
力扣70爬楼梯
数据结构·算法·leetcode
Navigator_Z3 天前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode