↓Skip to main content
  1. LeetCode/

LeetCode 3120: Count the Number of Special Characters I

·1 min· ·
LeetCode Easy Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2024-04-21

  • Submissions: Accepted (first try)

Approach
#

Split the seen letters into two sets, those seen in lowercase and those seen in uppercase (stored in one case), and count the letters present in both (Hash Table).

My Solution
#

class Solution:
    def numberOfSpecialChars(self, 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)

Complexity
#

Time O(n), space O(26).

Mistakes to Avoid
#

  • Store both sets in the same case before intersecting.

Interview Explanation
#

I put letters seen in lowercase and in uppercase into two sets in one case and return the size of their intersection. Time O(n).

Related

LeetCode 1160: Find Words That Can Be Formed by Characters
·1 min
LeetCode Easy Array Hash-Table String Counting
LeetCode 1160 note: a word is formable when none of its character counts exceeds chars’; sum the lengths of such words
LeetCode 804: Unique Morse Code Words
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 804 note: translate each word into a Morse string and count the distinct results with a set
LeetCode 500: Keyboard Row
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 500 note: make a set for each keyboard row and keep words whose lowercase character set is a subset of one row
LeetCode 13: Roman to Integer
·1 min
LeetCode Easy Hash-Table Math String
LeetCode 13 note: add each symbol’s value left to right, subtracting instead when it is smaller than the symbol to its right
LeetCode 1189: Maximum Number of Balloons
·2 mins
LeetCode Easy Hash-Table String Counting
LeetCode 1189 note: each letter supports count // needed balloons, with l and o needed twice; take the minimum
LeetCode 1002: Find Common Characters
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 1002 note: count each word and intersect all Counters (minimum count per character), then expand