Welcome to Subscribe On Youtube
3121. Count the Number of Special Characters II
Description
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 * 105
word
consists of only lowercase and uppercase English letters.
Solutions
Solution 1: Hash Table or Array
We define two hash tables or arrays first
and last
to store the positions where each letter first appears and last appears respectively.
Then we traverse the string word
, updating first
and last
.
Finally, we traverse all lowercase and uppercase letters. If last[a]
exists and first[b]
exists and last[a] < first[b]
, it means that the letter a
is a special letter, and we increment the answer by one.
The time complexity is $O(n + |\Sigma|)$, and the space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string word
, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| \leq 128$.
-
class Solution { public int numberOfSpecialChars(String word) { int[] first = new int['z' + 1]; int[] last = new int['z' + 1]; for (int i = 1; i <= word.length(); ++i) { int j = word.charAt(i - 1); if (first[j] == 0) { first[j] = i; } last[j] = i; } int ans = 0; for (int i = 0; i < 26; ++i) { if (last['a' + i] > 0 && first['A' + i] > 0 && last['a' + i] < first['A' + i]) { ++ans; } } return ans; } }
-
class Solution { public: int numberOfSpecialChars(string word) { vector<int> first('z' + 1); vector<int> last('z' + 1); for (int i = 1; i <= word.size(); ++i) { int j = word[i - 1]; if (first[j] == 0) { first[j] = i; } last[j] = i; } int ans = 0; for (int i = 0; i < 26; ++i) { if (last['a' + i] && first['A' + i] && last['a' + i] < first['A' + i]) { ++ans; } } return ans; } };
-
class Solution: def numberOfSpecialChars(self, word: str) -> int: first, last = {}, {} for i, c in enumerate(word): if c not in first: first[c] = i last[c] = i return sum( a in last and b in first and last[a] < first[b] for a, b in zip(ascii_lowercase, ascii_uppercase) )
-
func numberOfSpecialChars(word string) (ans int) { first := make([]int, 'z'+1) last := make([]int, 'z'+1) for i, c := range word { if first[c] == 0 { first[c] = i + 1 } last[c] = i + 1 } for i := 0; i < 26; i++ { if last['a'+i] > 0 && first['A'+i] > 0 && last['a'+i] < first['A'+i] { ans++ } } return }
-
function numberOfSpecialChars(word: string): number { const first: number[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => 0); const last: number[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => 0); for (let i = 0; i < word.length; ++i) { const j = word.charCodeAt(i); if (first[j] === 0) { first[j] = i + 1; } last[j] = i + 1; } let ans: number = 0; for (let i = 0; i < 26; ++i) { if ( last['a'.charCodeAt(0) + i] && first['A'.charCodeAt(0) + i] && last['a'.charCodeAt(0) + i] < first['A'.charCodeAt(0) + i] ) { ++ans; } } return ans; }