Introduction
LeetCode 3121 – Count the Number of Special Characters II is an interesting string and hashing problem that extends the logic of the previous Special Characters problem.
This problem tests:
- Character indexing
- String traversal
- HashMap usage
- Uppercase and lowercase handling
- Order-based conditions
Unlike Part I, this version adds an important restriction:
Every lowercase occurrence must appear before the first uppercase occurrence.
This additional condition makes the problem more challenging and interview-relevant.
Problem Link
🔗 https://leetcode.com/problems/count-the-number-of-special-characters-ii/
Problem Statement
You are given a string:
A character is called:
if:
- It appears in both lowercase and uppercase
- Every lowercase occurrence appears before the first uppercase occurrence
Return:
Example 1
Input
Output
Explanation
Special characters:
because:
- Lowercase letters appear first
- Uppercase letters appear later
Example 2
Input
Output:
No uppercase letters exist.
Example 3
Input
Output:
Explanation:
Lowercase letters appear after uppercase letters.
Condition fails.
Key Observation
For a character to be special:
This is the entire logic of the problem.
Intuition
We need two pieces of information.
For every character:
- Last occurrence of lowercase letter
- First occurrence of uppercase letter
If:
then character is special.
Brute Force Approach
Idea
For every character:
- Traverse entire string
- Find lowercase occurrences
- Find uppercase occurrences
- Verify ordering condition
Brute Force Complexity
Time Complexity
because repeated traversal is required.
Space Complexity
Optimized HashMap Approach
Idea
Use two HashMaps:
- Lowercase map stores last index
- Uppercase map stores first index
Then compare indices.
Java Solution
Cleaner Optimized Version
Why This Works
For every character:
We store:
and:
Then simply check:
If true:
Character satisfies problem condition.
Dry Run
Input
Step 1: Store Indices
Lowercase positions:
Uppercase positions:
Step 2: Compare
Count becomes:
Final Answer
Time Complexity Analysis
Optimized HashMap Solution
Time Complexity
because string traversal happens once.
Space Complexity
Maximum alphabet size remains fixed.
Brute Force vs Optimized
| Approach | Time Complexity | Space Complexity |
| Brute Force | O(N²) | O(1) |
| HashMap / Array Approach | O(N) | O(1) |
Interview Explanation
In interviews, explain:
A character is special only if its last lowercase occurrence appears before its first uppercase occurrence. We track indices efficiently using HashMaps or arrays.
This demonstrates:
- String manipulation skills
- Index tracking
- Hashing knowledge
- Optimization thinking
Common Mistakes
1. Checking Only Presence
Many solutions only verify:
But ordering condition is also required.
2. Storing Wrong Indices
We need:
- Last lowercase index
- First uppercase index
not the opposite.
3. Double Counting Characters
Always remove counted characters or use controlled traversal.
4. Ignoring Case Conversion
Always convert correctly using:
FAQs
Q1. Why store last lowercase occurrence?
Because all lowercase letters must appear before uppercase.
Q2. Why store first uppercase occurrence?
Because it defines the earliest uppercase appearance.
Q3. Can arrays replace HashMaps?
Yes.
Arrays are even more optimized because alphabet size is fixed.
Q4. Is this problem interview important?
Yes.
It tests:
- String traversal
- Index tracking
- Character handling
- Optimization techniques
Related Problems
Practice these next:
Conclusion
LeetCode 3121 is an excellent medium-level hashing and string problem.
It teaches:
- Efficient index tracking
- HashMap usage
- Order-based validation
- Character manipulation
The key insight is:
A character is special only if its last lowercase occurrence appears before its first uppercase occurrence.
Once this pattern becomes clear, many advanced string indexing problems become much easier.





