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

思路

  • 位统计

  • 合成Long统计

代码

  • 位统计

class Solution {
    public int numberOfSpecialChars(String word) {
        int small = 0;
        int large = 0;
        for (char c : word.toCharArray()) {
            if (c > 96) {
                small |= (1 << (c - 97));
            } else {
                large |= (1 << (c - 65));
            }
        }
        return Integer.bitCount(small & large);
    }
}
  • 合成Long统计

class Solution {
    public int numberOfSpecialChars(String word) {
        long flags = 0;
        for (int i = 0; i < word.length(); i++) {
            flags |= 1L << (word.charAt(i) - 'A');
        }
        return Long.bitCount(flags & (flags >> 32));
    }
}