Problem Statement in English

You’re given a string word consisting of lowercase and uppercase English letters. A character is called a special character if both its lowercase and uppercase forms appear in word. For example, if word contains the characters 'a' and 'A', then both 'a' and 'A' are special characters, and contribute a score of $1$ to the result.

Return the total score of all special characters in word.


Approach

We can create a set of unique characters in the string. Then, we can iterate through the alphabet and check if both the lowercase and uppercase forms of each character are present in the set. If they are, we can increment our result by 1.

And we’re done!


Solution in Python


class Solution:
    def numberOfSpecialChars(self, word: str) -> int:
        actual = set(list(word))

        res = 0
        for i in range(26):
            if chr(ord("a") + i) in actual and chr(ord("A") + i) in actual: res += 1

        return res

Complexity

  • Time: $O(n)$
    Since we need to iterate through the string once to create the set of unique characters, and then we need to iterate through the alphabet to check for special characters.

  • Space: $O(n)$
    Since we need to store the unique characters in a set.


And we are done.