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

LeetCode-2309. Best English letters with both upper and lower case_ Python

編輯:Python
  • Give you a string of English letters s , Please find out and return to s The best English letters in . The returned letters must be in upper case . If there is no letter that meets the condition , Then return an empty string .

  • The uppercase and lowercase forms of the best English letters must be in s It appears that .

  • English letter b Than another English letter a Better premise is : In the English alphabet ,b stay a After that .

Example 1:

Input :s = “lEeTcOdE”
Output :“E”
explain :
Letter ‘E’ Is the only letter that appears in both uppercase and lowercase .

Example 2:

Input :s = “arRAzFif”
Output :“R”
explain :
Letter ‘R’ It is the best English letter in both uppercase and lowercase .
Be careful ‘A’ and ‘F’ Both uppercase and lowercase forms of , however ‘R’ Than ‘F’ and ‘A’ Better .

Example 3:

Input :s = “AbCdEfGhIjK”
Output :“”
explain :
There are no letters in both upper and lower case forms .

Tips :

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

Program code

class Solution:
def greatestLetter(self, s: str) -> str:
hash1 = Counter(s)
ans = []
for i in range(97, 123):
if hash1[chr(i)] > 0 and hash1[chr(i - 32)] > 0:
ans.append(chr(i))
if len(ans) == 0:
return ""
return ans[len(ans) - 1].upper()

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