Count Letters with Ordered Lowercase and Uppercase Occurrences
Problem statement
You are given a string letters containing uppercase and lowercase English letters.
Count the different letters that satisfy both conditions:
- The letter appears at least once in lowercase and at least once in uppercase.
- Every lowercase occurrence of that letter appears before every uppercase occurrence of the same letter.
Return the number of qualifying letters. Count each letter at most once.
Function
solution(letters: String) → intExamples
Example 1
letters = "aaAbcCABBc"return = 2The letters a and b qualify. A lowercase c occurs after an uppercase C, so c does not qualify.
Example 2
letters = "abcABC"return = 3All three lowercase letters precede their uppercase occurrences.
Example 3
letters = "abABCdef"return = 2Only a and b appear in both cases. The other letters fail the presence condition.
Example 4
letters = "abcABCabc"return = 0Each of a, b, and c has a lowercase occurrence after its uppercase occurrence.
Constraints
1 <= letters.length <= 200000.letterscontains only English lettersa-zandA-Z.