Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 30 God , Click to see the event details
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.
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.
Note:
1 <= s.length <= 1000
s consists of lowercase and uppercase English letters.
According to the meaning , Given a string of English letters s , Back in the s The upper and lower case English letters appear in the . The returned letters should be capitalized . If there is no such letter , Then return an empty string . If b Appear in the English alphabet a after , Then the English letter b Greater than another letter a .
This question actually tests the dictionary order of letters , We traverse in reverse order 26 Lowercase letters , If it's in s There has been , And its capital letters are in s There has been , Then it means that the letter has the largest lexicographic order and its case exists in s Letters in , We return its capital letters .
The time complexity is O(26*N) , The space complexity is O(1) .
class Solution(object):
def greatestLetter(self, s):
"""
:type s: str
:rtype: str
"""
for i in range(ord('a')+25, ord('a')-1, -1):
c = chr(i)
if c in s and c.upper() in s:
return c.upper()
return ''
113 / 113 test cases passed.
Status: Accepted
Runtime: 17 ms
Memory Usage: 13.5 MB
leetcode.com/contest/wee…
Your support is my greatest motivation