Leetcode每日一题 —— 3120. 统计特殊字母的数量 I

Leetcode每日一题 —— 3120. 统计特殊字母的数量 I
Leetcode每日一题 —— 3120. 统计特殊字母的数量 I
力扣 LeetCode

3120. 统计特殊字母的数量 I - 力扣(LeetCode)

3120. 统计特殊字母的数量 I - 给你一个字符串 word。如果 word 中同时存在某个字母的小写形式和大写形式,则称这个字母为 特殊字母 。 返回 word 中 特殊字母 的数量。   示例 1: 输入:word = "aaAbcBC" 输出:3 解释: word 中的特殊字母是 'a'、'b' 和 'c'。 示例 2: 输入:word = "abc" 输出:0 解释: word 中不存在大小写形式同时出现的字母。 示例 3: 输入:word =...

思路
按题目要求记录好大小写是否出现过就好。

代码

class Solution {
    public int numberOfSpecialChars(String word) {
        int n = word.length();
        int[] cnt = new int[26];
        for (int i = 0; i < n; i++) {
            int o = word.charAt(i) - 'A';
            if (o > 26) {
                cnt[o - 32] |= 2;
            } else {
                cnt[o] |= 1;
            }
        }
        int ans = 0;
        for (int i = 0; i < 26; i++) {
            if (cnt[i] == 3) {
                ans++;
            }
        }
        return ans;
    }
}

2 个帖子 - 2 位参与者

阅读完整话题

来源: LinuxDo 最新话题查看原文