100294. 统计特殊字母的数量 I

 

100294. 统计特殊字母的数量 I

 
 
  • 通过的用户数3108
  • 尝试过的用户数3161
  • 用户总通过次数3158
  • 用户总提交次数4364
  • 题目难度Easy

给你一个字符串 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 仅由小写和大写英文字母组成。

Java
 
 
 
 
1
 
class Solution {
2
 
    public int numberOfSpecialChars(String word) {
3
         int res = 0;
4
        Set<Character> lowerSet = new HashSet<>();
5
        Set<Character> upperSet = new HashSet<>();
6
 
        for(int i =0;i<word.length();i++){
7
            char c = word.charAt(i);
8
 
            if(c>='a' && c <='z') {
9
                lowerSet.add(c);
10
            }
11
 
            else if (c>='A' && c<='Z'){
12
                upperSet.add(c);
13
            }
14
        }
15
 
        for(Character item : lowerSet){
16
            char upperChar = (char) (item - 32);
17
 
            if (upperSet.contains(upperChar)){
18
                res += 1;
19
            }
20
        }
21
        return res;
22
    }
23
}
posted @ 2024-04-21 12:01  Heinrich♣  阅读(30)  评论(0编辑  收藏  举报