You are given a string word. A letter c is called special if it appears both in lowercase and uppercase in word, and every lowercase occurrence of c appears before the first uppercase occurrence of c.
Return the number of special letters in word.
Example 1:
Input: word = "aaAbcBC"
Output: 3
Explanation:
The special characters are 'a', 'b', and 'c'.
Example 2:
Input: word = "abc"
Output: 0
Explanation:
There are no special characters in word.
Example 3:
Input: word = "AbBCab"
Output: 0
Explanation:
There are no special characters in word.
Constraints:
1 <= word.length <= 2 * 105word consists of only lowercase and uppercase English letters.class Solution {
public int numberOfSpecialChars(String word) {
boolean[] lower = new boolean[26];
boolean[] upper = new boolean[26];
for (char c : word.toCharArray()) {
if (c < 'a') // uppercase
upper[c - 'A'] = true;
else { // lowercase makes unspecial if occuring after uppercase
lower[c - 'a'] = !upper[c - 'a'];
}
}
int count = 0;
for (int i = 0; i < 26; i++) {
if (lower[i] && upper[i])
count++;
}
return count;
}
}