Count the Number of Special Characters I - Problem
You are given a string word. Your task is to find all the special characters in this string.
A character is considered special if it appears in both lowercase and uppercase forms within the same string. For example, if a string contains both 'a' and 'A', then the character 'a'/'A' is special.
Goal: Return the total count of special characters found in the string.
Example: In the string "AbBcC", we have:
- 'A' and 'a' - not both present
- 'B' and 'b' - both present โ
- 'C' and 'c' - not both present
So the answer is 1 (only 'B'/'b' qualifies as special).
Input & Output
example_1.py โ Basic Case
$
Input:
word = "aaAbcBC"
โบ
Output:
3
๐ก Note:
The special characters are 'a'/'A', 'b'/'B', and 'c'/'C'. All three appear in both lowercase and uppercase forms, so we return 3.
example_2.py โ Partial Matches
$
Input:
word = "abc"
โบ
Output:
0
๐ก Note:
None of the characters 'a', 'b', 'c' have their uppercase counterparts 'A', 'B', 'C' in the string, so no characters are special.
example_3.py โ Mixed Case
$
Input:
word = "AbBcC"
โบ
Output:
2
๐ก Note:
The characters 'b'/'B' and 'c'/'C' both appear in lowercase and uppercase forms. Character 'a'/'A' only appears as 'A' (uppercase), so it's not special. Result: 2.
Constraints
- 1 โค word.length โค 1000
- word consists of only English letters (both lowercase and uppercase)
- Characters can repeat multiple times in the string
- Empty string is not a valid input
Visualization
Tap to expand
Understanding the Visualization
1
Meet each person
As you encounter each character, note them down in your detective notebook (hash set)
2
Look for their twin
For each new person, immediately check if their twin (opposite case) is already in your notebook
3
Mark the pair
When you find a complete twin pair, mark it as 'found' and increment your counter
4
Avoid double counting
Make sure you don't count the same twin pair twice by keeping track of already counted pairs
Key Takeaway
๐ฏ Key Insight: Like a detective tracking twin pairs, we use a hash set to remember who we've met and immediately check for their twins (opposite case). The moment we find both twins, we've discovered a special character pair!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code