[Solved] Greatest English Letter in Upper and Lower Case Contest Problem

Greatest English Letter in Upper and Lower Case: Given a string of English letters s, return the greatest English letter which occurs as both a lowercase and uppercase letter in s. The returned letter should be in uppercase. If no such letter exists, return an empty string.

An English letter b is greater than another letter a if b appears after a in the English alphabet.

Greatest English Letter in Upper and Lower Case LeetCode

Example 1:

Input: s = "lEeTcOdE"
Output: "E"
Explanation:
The letter 'E' is the only letter to appear in both lower and upper case.

Example 2:

Input: s = "arRAzFif"
Output: "R"
Explanation:
The letter 'R' is the greatest letter to appear in both lower and upper case.
Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.

Example 3:

Input: s = "AbCdEfGhIjK"
Output: ""
Explanation:
There is no letter that appears in both lower and upper case.

Constraints:

  • 1 <= s.length <= 1000
  • s consists of lowercase and uppercase English letters.

Solution

class Solution {
        public String greatestLetter(String s) {
        boolean[] UpperCase = new boolean[26];
        boolean[] LowerCase = new boolean[26];
        char[] ch = s.toCharArray();
        for (char c : ch) {
            if (Character.isUpperCase(c)) {
                UpperCase[c - 'A'] = true;
            } else {
                LowerCase[c - 'a'] = true;
            }
        }
        for (int i = 25; i >= 0; i--) {
            if (UpperCase[i] && LowerCase[i]) {
                return (char) (i + 'A') + "";

            }
        }
        return "";
    }
}
class Solution {
public:
    string greatestLetter(string s) {
    string ans="";
    pair<bool,bool> cnt[26];
    for(auto i:s){
	    char up= toupper(i), lw= tolower(i);
        if(islower(i)) cnt[lw-'a'].first=true;     else cnt[lw-'a'].second=true;
		//if both are true
        if(cnt[lw-'a'].first and cnt[lw-'a'].second) ans= max(ans,{up});
    }
    return ans;
}
};
class Solution:
    def greatestLetter(self, s: str) -> str:
        
        if not any(char.islower() for char in s):
            return ""
        if not any(char.isupper() for char in s):
            return ""
        
        res = ''
        for char in s:
            if char.lower() in s and char.upper() in s:
                if char.isupper():
                    res += char
                
        res = "".join(sorted(res))
        
        if not res:
            return ""
        else:
            return res[-1]

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *