LeetCode 3120: Count the Number of Special Characters I – Java HashSet Solution Explained

Learn how to solve LeetCode 3120 Count the Number of Special Characters I using HashSet in Java. Includes intuition, brute force approach, optimized solution, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
1 views
LinkedInGithubX
0
0
LeetCode 3120: Count the Number of Special Characters I – Java HashSet Solution Explained
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 3120 – Count the Number of Special Characters I is a beginner-friendly string and hashing problem.

This problem focuses on:

  1. Character manipulation
  2. Uppercase and lowercase conversion
  3. HashSet usage
  4. String traversal
  5. Basic optimization techniques

It is a good interview problem for testing:

  1. Understanding of ASCII characters
  2. Java Character methods
  3. Set operations
  4. Problem-solving logic

Problem Link

🔗 https://leetcode.com/problems/count-the-number-of-special-characters-i/

Problem Statement

You are given a string:

word

A character is called:

special

if it appears in both:

  1. Lowercase
  2. Uppercase

Return:

Total number of special characters

Example 1

Input

word = "aaAbcBC"

Output

3

Explanation

Characters:

'a' and 'A'
'b' and 'B'
'c' and 'C'

All appear in both cases.

So answer is:

3

Example 2

Input

word = "abc"

Output:

0

No uppercase letters exist.

Example 3

Input

word = "abBCab"

Output:

1

Only:

'b' and 'B'

appear in both forms.

Intuition

We need to check:

For every lowercase character:

Does its uppercase version exist?

Using HashSet makes lookup very fast.

Brute Force Approach

Idea

For every character:

  1. Traverse entire string
  2. Search for uppercase/lowercase pair
  3. Count valid matches

Brute Force Complexity

Time Complexity

O(N²)

because nested traversal is required.

Space Complexity

O(1)

Optimized HashSet Approach

Idea

Use two sets:

  1. One for lowercase letters
  2. One for uppercase letters

Then check matching pairs.

Java Solution

class Solution {

public int numberOfSpecialChars(String word) {

HashSet<Character> lower = new HashSet<>();

HashSet<Character> upper = new HashSet<>();

for(int i = 0; i < word.length(); i++) {

if(word.charAt(i) >= 'a' && word.charAt(i) <= 'z') {
lower.add(word.charAt(i));
}
else {
upper.add(word.charAt(i));
}
}

int ans = 0;

for(int i = 0; i < word.length(); i++) {

char up = Character.toUpperCase(word.charAt(i));

if(lower.contains(word.charAt(i)) && upper.contains(up)) {

ans++;

lower.remove(word.charAt(i));
upper.remove(up);
}
}

return ans;
}
}

Cleaner Optimized Version

class Solution {

public int numberOfSpecialChars(String word) {

HashSet<Character> lower = new HashSet<>();
HashSet<Character> upper = new HashSet<>();

for(char ch : word.toCharArray()) {

if(Character.isLowerCase(ch)) {
lower.add(ch);
}
else {
upper.add(ch);
}
}

int count = 0;

for(char ch : lower) {

if(upper.contains(Character.toUpperCase(ch))) {
count++;
}
}

return count;
}
}

Why This Works

We separate:

  1. Lowercase characters
  2. Uppercase characters

Then for every lowercase letter:

We check whether uppercase version exists.

HashSet lookup works in:

O(1)

average time.

Dry Run

Input

word = "aaAbcBC"

Step 1

Lowercase set:

[a, b, c]

Uppercase set:

[A, B, C]

Step 2

Check:

'a' → 'A' exists
'b' → 'B' exists
'c' → 'C' exists

Count becomes:

3

Final Answer

3

Time Complexity Analysis

Optimized HashSet Solution

Time Complexity

O(N)

because string traversal happens only once.

Space Complexity

O(1)

Maximum alphabet size is fixed:

26 lowercase + 26 uppercase

Brute Force vs Optimized

ApproachTime ComplexitySpace Complexity
Brute ForceO(N²)O(1)
HashSet ApproachO(N)O(1)

Interview Explanation

In interviews, explain:

We use two HashSets to separately store lowercase and uppercase letters. Then we check whether a lowercase character has its uppercase counterpart.

This demonstrates:

  1. Efficient lookup usage
  2. Hashing knowledge
  3. Character manipulation skills

Common Mistakes

1. Double Counting Characters

Without removing counted characters:

same letter may be counted multiple times

2. Forgetting Case Conversion

Always convert:

Character.toUpperCase(ch)

before comparison.

3. Using Nested Loops Unnecessarily

HashSet reduces lookup complexity significantly.

FAQs

Q1. Why use HashSet?

Because lookup operations are very fast.

Q2. Can this be solved without extra space?

Yes.

Using arrays of size 26 is also possible.

Q3. Why remove characters after counting?

To avoid duplicate counting.

Q4. Is this problem important for interviews?

Yes.

It tests:

  1. String handling
  2. Character conversion
  3. HashSet usage
  4. Optimization thinking

Related Problems

Practice these next:

  1. Valid Anagram
  2. First Unique Character in a String
  3. Ransom Note

Conclusion

LeetCode 3120 is a simple but effective problem for learning:

  1. HashSet operations
  2. String traversal
  3. Case conversion
  4. Efficient lookup techniques

The key idea is:

Store lowercase and uppercase letters separately, then check matching pairs efficiently.

Once this pattern becomes clear, many string hashing problems become much easier.

Ai Assistant Kas