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
๐Ÿ•ต๏ธ The Special Character Detective Story๐Ÿ•ต๏ธDetectiveInvestigating string: "AbBcC"Step 1: Meeting the CharactersAPerson AbPerson bBPerson BcPerson cCPerson CStep 2-3: Finding Twin PairsbBTwin Pair 1 โœ“cCTwin Pair 2 โœ“ANo twin 'a' โœ—๐ŸŽฏ Detective's ReportFound 2 twin pairs: (b,B) and (c,C)The key insight: We only care about presence, not position or frequency!
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!
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
25.8K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen