Skip to content

Practice Count the Number of Special Characters I

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#3120
LeetCode Problem

Count the Number of Special Characters I

Problem Statement

Count the distinct English letters that occur in both cases within word. A letter appearing several times still contributes at most one. In this version, the order of uppercase and lowercase occurrences does not matter. Practice approach: Record the lowercase and uppercase forms in separate sets, then count letters present in both. Two 26-bit masks are another option. The scan takes O(n) time and O(1) bounded alphabet storage.

Source: Count the Number of Special Characters I on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
word = "aaAbcBC"
Output
3

# Example 2

Input
word = "abc"
Output
0

# Example 3

Input
word = "abBCab"
Output
1

Constraints

1 <= word.length <= 50
word consists of only lowercase and uppercase English letters.

How to solve Count the Number of Special Characters I

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Because order is irrelevant here, the question is pure set membership: which letters appear in lowercase and which in uppercase. The answer is the size of the intersection, which two 26-bit masks compute with one AND and a popcount.

Why brute force is not enough

Scanning the whole word for each of the 26 letters in both cases is O(26 * n); trivial for n at most 50, but it rereads the input dozens of times. A nested loop comparing every pair of characters is O(n^2) and double counts repeated letters.

Approach

  1. Allocate two presence trackers, one per case, each a 26-slot boolean array or a single integer bitmask.
  2. Scan word once, setting the bit for a lowercase character at its offset from a in the lowercase tracker and for an uppercase character at its offset from A in the uppercase tracker. Each tracker records exactly the letters seen so far in that case, regardless of repetition.
  3. Intersect the two trackers, by ANDing the masks or iterating 26 positions, and count positions set in both.
  4. Return that count; letters present in one case only contribute nothing.

Walkthrough of Example 1

word = "aaAbcBC" → 3

  1. Scan aaAbcBC: a sets lowercase a, the second a changes nothing, A sets uppercase A.
  2. b and c set lowercase b and c; B and C set uppercase B and C.
  3. Lowercase set is {a, b, c}, uppercase set is {A, B, C}.
  4. The intersection has three letters, so the answer is 3.

Time and space

Time O(n) · Space O(1). One pass over the n characters with constant work each; both trackers are bounded by the 26-letter alphabet.

Interview pitfalls

  • Counting occurrences instead of distinct letters, so aAaA reports 2 or 4 instead of 1.
  • Computing the offset from the wrong base, for example subtracting a from an uppercase character and indexing out of range.
  • Returning early with 1 upon finding any letter in both cases rather than counting all such letters.
  • Importing the part II ordering rule, which would wrongly reject inputs like Aa in this version.

Follow-up questions interviewers ask

  • How does the solution change in part II, where every lowercase occurrence must precede every uppercase occurrence of the same letter?
  • How would you support a stream of characters with a query for the current count after each insertion?
  • Can you generalize to an alphabet where case pairing is given by a lookup table rather than ASCII arithmetic?

Further reading on the technique: Bit array

Related LeetCode Problems

Practice more problems similar to Count the Number of Special Characters I.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.