Count the Number of Special Characters I - Problem

You are given a string word. A letter is called special if it appears both in lowercase and uppercase in word.

Return the number of special letters in word.

Input & Output

Example 1 — Mixed Case Letters
$ Input: word = "aaAbcBC"
Output: 3
💡 Note: Letters 'a', 'b', and 'c' each appear in both lowercase and uppercase forms
Example 2 — Only Lowercase
$ Input: word = "abc"
Output: 0
💡 Note: All letters are lowercase only, no uppercase versions present
Example 3 — Partial Match
$ Input: word = "abBcab"
Output: 1
💡 Note: Only letter 'b' appears in both cases (lowercase 'b' and uppercase 'B')

Constraints

  • 1 ≤ word.length ≤ 1000
  • word consists of only lowercase and uppercase English letters.

Visualization

Tap to expand
Count the Number of Special Characters I INPUT word = "aaAbcBC" a a A b c B C Lowercase Uppercase Character Analysis Char Lower Upper a/A Yes Yes b/B Yes Yes c/C Yes Yes ALGORITHM STEPS 1 Create Sets Track lowercase & uppercase lowerSet {a, b, c} upperSet {A, B, C} 2 Iterate String Add each char to its set 3 Check Each Letter For a-z, check both cases for c in 'a' to 'z': if c in lowerSet and c.upper in upperSet: count += 1 4 Return Count Special letters found FINAL RESULT Special Characters Found: A a + A B b + B C c + C Output 3 3 special letters found! Verification Letter Both? Special a/A Yes OK b/B Yes OK c/C Yes OK Key Insight: A letter is "special" if it appears in BOTH lowercase AND uppercase forms in the string. Using two sets (one for lowercase, one for uppercase) allows O(1) lookup for each character. Time Complexity: O(n) where n is string length | Space Complexity: O(1) - max 52 characters in sets TutorialsPoint - Count the Number of Special Characters I | Optimal Solution
Asked in
Google 15 Microsoft 12
12.5K Views
Medium Frequency
~10 min Avg. Time
245 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