3121. Count the Number of Special Characters II LeetCode Solution
In this guide, you will get 3121. Count the Number of Special Characters II LeetCode Solution with the best time and space complexity. The solution to Count the Number of Special Characters II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count the Number of Special Characters II solution in C++
- Count the Number of Special Characters II solution in Java
- Count the Number of Special Characters II solution in Python
- Additional Resources

Problem Statement of Count the Number of Special Characters II
You are given a string word. A letter c is called special if it appears both in lowercase and uppercase in word, and every lowercase occurrence of c appears before the first uppercase occurrence of c.
Return the number of special letters in word.
Example 1:
Input: word = “aaAbcBC”
Output: 3
Explanation:
The special characters are ‘a’, ‘b’, and ‘c’.
Example 2:
Input: word = “abc”
Output: 0
Explanation:
There are no special characters in word.
Example 3:
Input: word = “AbBCab”
Output: 0
Explanation:
There are no special characters in word.
Constraints:
1 <= word.length <= 2 * 105
word consists of only lowercase and uppercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(26) = O(1)
3121. Count the Number of Special Characters II LeetCode Solution in C++
class Solution {
public:
int numberOfSpecialChars(string word) {
int ans = 0;
vector<bool> lower(26);
vector<bool> upper(26);
for (const char c : word)
if (islower(c))
lower[c - 'a'] = !upper[c - 'a'];
else
upper[c - 'A'] = true;
for (int i = 0; i < 26; ++i)
if (lower[i] && upper[i])
++ans;
return ans;
}
};
/* code provided by PROGIEZ */
3121. Count the Number of Special Characters II LeetCode Solution in Java
class Solution {
public int numberOfSpecialChars(String word) {
int ans = 0;
boolean[] lower = new boolean[26];
boolean[] upper = new boolean[26];
for (final char c : word.toCharArray())
if (Character.isLowerCase(c))
lower[c - 'a'] = !upper[c - 'a'];
else
upper[c - 'A'] = true;
for (int i = 0; i < 26; ++i)
if (lower[i] && upper[i])
++ans;
return ans;
}
}
// code provided by PROGIEZ
3121. Count the Number of Special Characters II LeetCode Solution in Python
class Solution:
def numberOfSpecialChars(self, word: str) -> int:
lower = collections.defaultdict(bool)
upper = collections.defaultdict(bool)
for c in word:
if c.islower():
lower[c] = not upper[c.upper()]
else:
upper[c] = True
return sum(lower[a] and upper[b]
for a, b in zip(string.ascii_lowercase,
string.ascii_uppercase))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.