← All solutions
May 26Easy

Count the number of special characters i

  • Hash Table
  • String

Problem

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.

Solution

class Solution:
    def numberOfSpecialChars(self, word: str) -> int:
        count, lower, upper = 0, set(), set()
        for letter in word:
            if letter in lower or letter in upper:
                continue
            if "a" <= letter <= "z":
                if chr(ord(letter) + ord("A") - ord("a")) in upper:
                    count += 1
                lower.add(letter)
            else:
                if chr(ord(letter) - ord("A") + ord("a")) in lower:
                    count += 1
                upper.add(letter) 

        return count