↓Skip to main content
  1. LeetCode/

LeetCode 3121: Count the Number of Special Characters II

·2 mins· ·
LeetCode Medium 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: medium First Attempt: 2024-04-21

  • Submissions: Wrong Answer ×4 → Accepted

Approach
#

“Every lowercase occurrence precedes the first uppercase one” means last lowercase index < first uppercase index. One pass records both (Hash Table); a letter is special when both exist in that order.

My Attempts
#

It failed four times during the 2024 contest:

  • Attempt 1 checked w not in upper to see whether an uppercase letter was recorded, but w was uppercase while the keys were lowercase, so it always looked unrecorded and later uppercase letters overwrote the first index: "AaA" should be 0; it returned 1.
  • After switching to sets that tracked state, one version treated another uppercase after the lowercase as a violation: "aAA" should be 1; it returned 0.
  • The next fixed that but, although it recorded uppercase-before-lowercase, never used it to disqualify: "AaA" returned 1. Adding that check passed.

Solution
#

class Solution:
    def numberOfSpecialChars(self, word: str) -> int:
        last_lower = {}
        first_upper = {}
        for i, c in enumerate(word):
            if c.islower():
                last_lower[c] = i
            elif c.lower() not in first_upper:
                first_upper[c.lower()] = i
        return sum(1 for c, i in last_lower.items()
                   if c in first_upper and i < first_upper[c])

Complexity
#

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

Mistakes to Avoid
#

  • Record the first uppercase and the last lowercase index.
  • Use one case for the dict keys, or the “already recorded” check never fires.
  • Uppercase before lowercase (like "Aa") also disqualifies a letter.

Interview Explanation
#

In one pass I record each letter’s last lowercase index and first uppercase index, counting letters where both exist and the lowercase one comes first. Time O(n).

Related

LeetCode 187: Repeated DNA Sequences
·1 min
LeetCode Medium Hash-Table String Bit-Manipulation Sliding-Window
LeetCode 187 note: count every length-10 substring in a hash map and return those seen more than once
LeetCode 208: Implement Trie (Prefix Tree)
·2 mins
LeetCode Medium Hash-Table String Design Trie
LeetCode 208 note: each node is a children dict plus an is_end flag; insert, search and startsWith all walk down character by character
LeetCode 424: Longest Repeating Character Replacement
·2 mins
LeetCode Medium Hash-Table String Sliding-Window
LeetCode 424 note: a variable sliding window where window length minus the top character count is the number of replacements; shrink when it exceeds k
LeetCode 981: Time Based Key-Value Store
·2 mins
LeetCode Medium Hash-Table String Binary-Search Design
LeetCode 981 note: store a (timestamp, value) list per key, with timestamps increasing; get binary searches the last timestamp <= the query
LeetCode 49: Group Anagrams
·2 mins
LeetCode Medium Array Hash-Table String Sorting
LeetCode 49 note: use each string’s sorted form as a key; strings sharing a key form one anagram group
LeetCode 17: Letter Combinations of a Phone Number
·2 mins
LeetCode Medium Hash-Table String Backtracking
LeetCode 17 note: each level handles one digit and tries its letters; an empty input returns an empty list