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
.
Example 1:
Input: word = "aaAbcBC"
Output: 3
Explanation:
The special characters in word
are 'a'
, 'b'
, and 'c'
.
Example 2:
Input: word = "abc"
Output: 0
Explanation:
No character in word
appears in uppercase.
Example 3:
Input: word = "abBCab"
Output: 1
Explanation:
The only special character in word
is 'b'
.
Constraints:
1 <= word.length <= 50
word
consists of only lowercase and uppercase English letters.
Similar Questions:
Hints:
- The constraints are small. For all the 56 characters, check if they are present in
word
.
// OJ: https://leetcode.com/problems/count-the-number-of-special-characters-i
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
int numberOfSpecialChars(string s) {
int cnt[26] = {}, ans = 0;
for (char c : s) {
if (islower(c)) cnt[c - 'a'] |= 1;
else cnt[c - 'A'] |= 2;
}
for (int i = 0; i < 26; ++i) {
ans += cnt[i] == 3;
}
return ans;
}
};