Count the Number of Special Characters I

Easy
1814.

Count the Number of Special Characters I

Count the Number of Special Characters I is an easy coding problem on strings and hash table, asked in Amazon interviews.

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 <= 50
  • word consists of only lowercase and uppercase English letters
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} word
 * @return {number}
 */
var numberOfSpecialChars = function(word) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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 <= 50
  • word consists of only lowercase and uppercase English letters

Hints for Count the Number of Special Characters I

  1. Hint 1

    Record which lowercase letters occur and which uppercase letters occur — two sets of 26.

  2. Hint 2

    A letter is special when it is in both sets.

  3. Hint 3

    Count the letters from a to z that 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

  1. Scan word; for a lowercase c set lower[c - 'a'], for an uppercase c set upper[c - 'A'].
  2. Count indices i in 0..25 where both lower[i] and upper[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 — aaA has 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