# Solving Leetcode Interviews in Seconds with AI: Count the Number of Special Characters II


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "3121" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> You are given a string word. A letter c is called special if it appears both in lowercase and uppercase in word, and every lowercase occurrence of c appears before the first uppercase occurrence of c. Return the number of special letters in word.   Example 1:  Input: word = "aaAbcBC" Output: 3 Explanation: The special characters are 'a', 'b', and 'c'.  Example 2:  Input: word = "abc" Output: 0 Explanation: There are no special characters in word.  Example 3:  Input: word = "AbBCab" Output: 0 Explanation: There are no special characters in word.    Constraints:  1 <= word.length <= 2 * 105 word consists of only lowercase and uppercase English letters.  

	# Explanation
	Here's the breakdown of the solution:

*   **Identify Potential Special Characters:** Use sets to efficiently track lowercase and uppercase letters present in the word. The intersection of these sets gives us the potential special characters.
*   **Verify Order of Occurrence:** For each potential special character, check if all its lowercase occurrences precede the first uppercase occurrence.
*   **Count Special Characters:** Increment the count if the order condition is met.

*   **Runtime Complexity:** O(n), where n is the length of the word.
*   **Storage Complexity:** O(1), as the sets can store at most 26 characters each (English alphabet).

	
	# Code
	```python
	def count_special_characters(word: str) -> int:
    """
    Given a string word. A letter c is called special if it appears both in lowercase and uppercase in word, and every lowercase occurrence of c appears before the first uppercase occurrence of c.
    Return the number of special letters in word.

    Example 1:
    Input: word = "aaAbcBC"
    Output: 3
    Explanation: The special characters are 'a', 'b', and 'c'.

    Example 2:
    Input: word = "abc"
    Output: 0
    Explanation: There are no special characters in word.

    Example 3:
    Input: word = "AbBCab"
    Output: 0
    Explanation: There are no special characters in word.

    Constraints:
    1 <= word.length <= 2 * 105
    word consists of only lowercase and uppercase English letters.
    """

    lowercase_chars = set()
    uppercase_chars = set()

    for char in word:
        if 'a' <= char <= 'z':
            lowercase_chars.add(char)
        else:
            uppercase_chars.add(char)

    special_chars = 0
    potential_special_chars = lowercase_chars.intersection(
        {char.lower() for char in uppercase_chars})

    for char in potential_special_chars:
        first_uppercase_index = -1
        valid = True
        for i in range(len(word)):
            if word[i] == char:
                if first_uppercase_index != -1:
                    valid = False
                    break
            elif word[i] == char.upper():
                if first_uppercase_index == -1:
                    first_uppercase_index = i
        
        first_uppercase_index = -1
        for i in range(len(word)):
            if word[i] == char.upper():
                first_uppercase_index = i
                break
        
        for i in range(len(word)):
            if word[i] == char and first_uppercase_index != -1 and i > first_uppercase_index:
                valid = False
                break
                
        if valid:
            special_chars += 1

    return special_chars
	```
			
