The Count the Number of Special Characters II interview question is a more refined version of the "special character" check. A character is special if both its lowercase and uppercase versions exist in the string, but with a specific order requirement: every occurrence of the lowercase version must appear before the first occurrence of the uppercase version.
For example, in "aaAbA", 'a' is NOT special because the second 'a' is before the first 'A', but then there is another 'A' later. Wait, the rule is usually: all lowercase 'a's must be before the first uppercase 'A'. If "aaA" is the string, 'a' is special. If "aAa", 'a' is not.
Microsoft and Google use this "Medium" problem to test a candidate's ability to track multiple indices and validate chronological constraints. It evaluations hash table interview pattern skills, specifically using maps to store the "last seen" index of lowercase letters and the "first seen" index of uppercase letters. It’s a test of precise state management.
This problem uses Index Tracking with Hash Tables.
last_lower to store the last index of each lowercase character.first_upper to store the first index of each uppercase character.last_lower for every lowercase char and only update first_upper if the char hasn't been seen in uppercase yet.last_lower and first_upper.last_lower[c] < first_upper[c].s = "abcAbC"
last_lower: {a:0, b:1, c:2}first_upper: {A:3, C:5}When a problem specifies a "before/after" relationship between sets of indices, always focus on the boundary values: the last index of the "before" set and the first index of the "after" set.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| HTML Entity Parser | Medium | Solve | |
| Alphabet Board Path | Medium | Solve | |
| Can Convert String in K Moves | Medium | Solve | |
| Count the Number of Special Characters I | Easy | Solve | |
| Maximum Number of Words You Can Type | Easy | Solve |