Back to May Back to Year 2026
EASY 26 May 2026 View on LeetCode

3120. Count the Number of Special Characters I

</> Solution

/**
 * Problem: LeetCode 3120 - Count the Number of Special Characters I
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public int numberOfSpecialChars(String word) {
        int lowerMask = 0;
        int upperMask = 0;

        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            if (ch >= 'a' && ch <= 'z') {
                lowerMask |= (1 << (ch - 'a'));
            } else if (ch >= 'A' && ch <= 'Z') {
                upperMask |= (1 << (ch - 'A'));
            }
        }

        return Integer.bitCount(lowerMask & upperMask);
    }
}

TIME COMPLEXITY

O(N)

SPACE COMPLEXITY

O(1)

TOPICS

Bit Manipulation Hash Table String