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

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

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

3121. 统计特殊字母的数量 II - 给你一个字符串 word。如果 word 中同时出现某个字母 c 的小写形式和大写形式,并且 每个 小写形式的 c 都出现在第一个大写形式的 c 之前,则称字母 c 是一个 特殊字母 。 返回 word 中 特殊字母 的数量。   示例 1: 输入:word = "aaAbcBC" 输出:3 解释: 特殊字母是 'a'、'b' 和 'c'。 示例 2: 输入:word = "abc" 输出:0 解释: 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) {
                if ((cnt[o - 32] & 1) == 1) {
                    cnt[o - 32] |= 4;
                }
                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;
    }
}

1 个帖子 - 1 位参与者

阅读完整话题

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