[LeetCode] Count the Number of Special Characters I

3120. Count the Number of Special Characters I

You are given a string word. A letter is called special if it appears both in lowercase and uppercase in word.

Return the number of special letters in word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int numberOfSpecialChars(string word) {
long lo = 0, up = 0;
auto masking = [](long& mask, int bit) {
mask |= 1ll<<bit;
};
for(auto& w : word) {
if(islower(w)) masking(lo,w-'a');
else masking(up,w-'A');
}
return __builtin_popcountll(lo & up);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/04/21/PS/LeetCode/count-the-number-of-special-characters-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.