Count the Number of Special Characters I — Solution
Count the Number of Special Characters I solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Count the Number of Special Characters I is an easy coding problem on strings and hash table, asked in Amazon interviews.
- Difficulty: Easy
- Topics: Strings, Hash Table
- Asked at: Amazon
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A letter is special in word if it appears in word both in lowercase and in uppercase.
Return the number of special letters in word.
Example 1
Input: word = "aaAbcBC"
Output: 3
Explanation: `a`, `b` and `c` each appear in both cases.
Example 2
Input: word = "abc"
Output: 0
Explanation: No letter appears in uppercase.
Example 3
Input: word = "abBCab"
Output: 1
Explanation: Only `b` appears in both cases; `C` has no lowercase `c`.
Constraints
1 <= word.length <= 50word consists of only lowercase and uppercase English letters
Hints for Count the Number of Special Characters I
Hint 1
Record which lowercase letters occur and which uppercase letters occur — two sets of 26.
Hint 2
A letter is special when it is in both sets.
Hint 3
Count the letters from
atozthat are marked in both.
How to solve Count the Number of Special Characters I
Track presence per letter in each case with two 26-entry tables, then count the letters present in both.
Approach
- Scan
word; for a lowercasecsetlower[c - 'a'], for an uppercasecsetupper[c - 'A']. - Count indices
iin0..25where bothlower[i]andupper[i]are set.
Why it works
A letter is special exactly when at least one lowercase and one uppercase occurrence exist, which is what the two tables record.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Count distinct letters, not occurrences —
aaAhas one special letter.
Reference solution
Python
def numberOfSpecialChars(word: str) -> int:
lower = {c for c in word if c.islower()}
upper = {c.lower() for c in word if c.isupper()}
return len(lower & upper)Java
public static int numberOfSpecialChars(String word) {
boolean[] lower = new boolean[26], upper = new boolean[26];
for (char c : word.toCharArray()) {
if (Character.isLowerCase(c)) lower[c - 'a'] = true; else upper[c - 'A'] = true;
}
int count = 0;
for (int i = 0; i < 26; i++) if (lower[i] && upper[i]) count++;
return count;
}C++
int numberOfSpecialChars(string word) {
bool lower[26] = {false}, upper[26] = {false};
for (char c : word) {
if (c >= 'a') lower[c - 'a'] = true; else upper[c - 'A'] = true;
}
int count = 0;
for (int i = 0; i < 26; i++) if (lower[i] && upper[i]) count++;
return count;
}JavaScript
var numberOfSpecialChars = function(word) {
var lower = new Array(26).fill(false), upper = new Array(26).fill(false);
for (var i = 0; i < word.length; i++) {
var c = word.charCodeAt(i);
if (c >= 97) lower[c - 97] = true; else upper[c - 65] = true;
}
var count = 0;
for (var k = 0; k < 26; k++) if (lower[k] && upper[k]) count++;
return count;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings · Hashing: Hash Maps and Hash Sets