Problem Description
You are given a string word. A letter is called special if it appears in both lowercase and uppercase in word. Return the number of special letters in word.
Key Insights
- Use two sets: one for lowercase characters and one for uppercase characters.
- A letter is special if its lowercase version exists in both sets.
- The input size is small (maximum 50 characters), so a simple iteration over the string is efficient.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the string word. Space Complexity: O(1) since we only store at most 26 lowercase and 26 uppercase characters.
Solution
We iterate through the string and add characters to two separate sets (one for lowercase and one for uppercase). Then, we iterate through one of the sets (preferably lowercase) and check if the corresponding uppercase version is present in the uppercase set. Each match indicates a special character. The count of these matches is returned as the result.