程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

leetcode 2309. Greatest English Letter in Upper and Lower Case(python)

編輯:Python

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

describe

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.

analysis

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) .

answer

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 ''

Running results

113 / 113 test cases passed.
Status: Accepted
Runtime: 17 ms
Memory Usage: 13.5 MB

Original link

leetcode.com/contest/wee…

Your support is my greatest motivation


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved