Talentd
FleetCode

3121. Count the Number of Special Characters II

Medium60.3% acceptance3 companies

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.

Asked at 3 companies

Hints open one at a time

Hint 1
For each character c, store the first occurrence of its uppercase and the last occurrence of its lowercase.
14:27
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def numberOfSpecialChars(self, word: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"aaAbcBC"
Input
"abc"
Input
"AbBCab"
Your code is saved on this device.Solution