leetcodedaybyday - Beats 100% with C++ and Beats 100% with Python3
IntuitionThe goal is to find the greatest English letter (A-Z) that appears in both uppercase and lowercase forms in the string. The problem is based on matchin
# Intuition
The goal is to find the greatest English letter (A-Z) that appears in both uppercase and lowercase forms in the string. The problem is based on matching corresponding uppercase and lowercase characters while ensuring they exist in the string.

# Approach
1. **Frequency Count**:
   - Use two frequency arrays (or equivalent checks) to count the occurrences of lowercase and uppercase letters.

2. **Iterate from Largest to Smallest**:
   - Check the letters from 'Z' to 'A'. If both the uppercase and lowercase forms of a letter exist, it qualifies as the greatest letter.

3. **Optimization**:
   - Instead of using arrays, directly check for the presence of both uppercase and lowercase forms of each letter using Python's `in` operator or similar logic in other languages.

4. **Return the Result**:
   - Return the first valid uppercase letter that satisfies the condition. If no such letter exists, return an empty string.

# Complexity
- **Time Complexity**:
  - \(O(n)\), where \(n\) is the length of the string. We scan the string and perform up to 26 checks.

- **Space Complexity**:
  - \(O(1)\), as the space used for frequency arrays or character checks is constant.
---
# Code
```cpp []
class Solution {
public:
    string greatestLetter(string s) {
        string ans = "";
        vector<int>freqlower(26, 0);
        vector<int>frequpper(26, 0);
        for (int i = 0; i < s.size(); i ++){
            int idx;
            if (s[i] >= 'a' && s[i] <= 'z'){
                idx = int(s[i] - 'a');
                freqlower[idx]++;
            } else {
                idx = int(s[i] - 'A');
                frequpper[idx]++;
            }
        }
        for (int i = 25; i >= 0; i--){
            if(frequpper[i] > 0 && freqlower[i] > 0){
                ans = char(i + 'A');
                break;
            }
        }
        return ans;
    }
};


```
```python3 []
class Solution:
    def greatestLetter(self, s: str) -> str:
        # freqLower = [0] * 26
        # freqUpper = [0] * 26
        # ans = ""
        # for c in s:
        #     if 'a' <= c <= 'z':
        #         idx = ord(c) - ord('a')
        #         freqLower[idx] += 1
        #     elif 'A' <= c <= 'Z':
        #         idx = ord(c) - ord('A')
        #         freqUpper[idx] += 1
        # for i in range(25, -1, -1):
        #     if freqUpper[i] > 0 and freqLower[i] > 0:
        #         ans = chr(i + ord('A'))
        #         break
        # return ans

        for i in range(25, -1, -1):
            lower = chr(i + ord('a'))
            upper = chr(i + ord('A'))
            if lower in s and upper in s:
                return upper
        return ''



```


['C++', 'Python3']