Count letters whose lowercase copies all come before uppercase
Given a string s of English letters (it may also contain other characters, which you ignore), count the distinct letters, compared case-insensitively, that satisfy this rule:
Every lowercase occurrence of the letter appears before every uppercase occurrence of the same letter.
A letter that appears only in lowercase, or only in uppercase, satisfies the rule.
Implement:
def count_ordered_letters(s: str) -> int: ...
Constraints
0 <= len(s) <= 10^5- Aim for O(n) time.
Example 1
Input: s = "xxXyYyz"
Output: 2
x: both lowercase copies come before X, so it counts. y: a lowercase y follows Y, so it does not. z: lowercase only, so it counts.
Example 2
Input: s = "Hello hELLO"
Output: 3
e, l and o count. h does not, because a lowercase h follows H.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
