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 * 105wordconsists of only lowercase and uppercase English letters.
Solution
Python3
class Solution:
def numberOfSpecialChars(self, word: str) -> int:
A = [[-1, -1] for _ in range(26)]
for i, x in enumerate(word):
if x.isupper():
if A[ord(x) - ord('A')][1] == -1:
A[ord(x) - ord('A')][1] = i
else:
A[ord(x) - ord('a')][0] = i
res = 0
for i in range(26):
if A[i][0] != -1 and A[i][1] != -1 and A[i][0] < A[i][1]:
res += 1
return res