3120. Count the Number of Special Characters I LeetCode Solution

In this guide, you will get 3120. Count the Number of Special Characters I LeetCode Solution with the best time and space complexity. The solution to Count the Number of Special Characters I 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

  1. Problem Statement
  2. Complexity Analysis
  3. Count the Number of Special Characters I solution in C++
  4. Count the Number of Special Characters I solution in Java
  5. Count the Number of Special Characters I solution in Python
  6. Additional Resources
3120. Count the Number of Special Characters I LeetCode Solution image

Problem Statement of Count the Number of Special Characters I

You are given a string word. A letter is called special if it appears both in lowercase and uppercase in word.
Return the number of special letters in word.

Example 1:

Input: word = “aaAbcBC”
Output: 3
Explanation:
The special characters in word are ‘a’, ‘b’, and ‘c’.

Example 2:

Input: word = “abc”
Output: 0
Explanation:
No character in word appears in uppercase.

Example 3:

Input: word = “abBCab”
Output: 1
Explanation:
The only special character in word is ‘b’.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(26) = O(1)

3120. Count the Number of Special Characters I 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'] = true;
      else
        upper[c - 'A'] = true;

    for (int i = 0; i < 26; ++i)
      if (lower[i] && upper[i])
        ++ans;

    return ans;
  }
};
/* code provided by PROGIEZ */

3120. Count the Number of Special Characters I 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'] = true;
      else
        upper[c - 'A'] = true;

    for (int i = 0; i < 26; ++i)
      if (lower[i] && upper[i])
        ++ans;

    return ans;
  }
}
// code provided by PROGIEZ

3120. Count the Number of Special Characters I 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] = True
      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

See also  1693. Daily Leads and Partners LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.