Skip to content

Commit 8126a4e

Browse files
committed
chore: add daily leetcode post 2309兼具大小写的最好英文字母_translated
1 parent 4b1a0aa commit 8126a4e

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: 2309. The best English letters with both appropriates and lowercases One question daily
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- - Python
6+
- - answer
7+
- - One question daily
8+
- - Bit operation
9+
- - Hash table
10+
- - C++
11+
abbrlink: b4953d62
12+
---
13+
14+
[2309. The best English letters with both appropriates and lowercases](https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/description/)
15+
16+
# Thought:
17+
18+
1. Hash table:The first thing to think of the question is to solve it with a dictionary。But find that you can directly traverse the alphabet,fromZStart looking for,See if the applause exists,Just return directly。
19+
20+
*Running time exceeds99.9%*
21+
2. Bit operation:We can use two integers mask1 and mask2 Record string separately s 中出现的小写字母and大写字母,in mask1 First i
22+
Position representation i Does a lowercase letter appear,and mask2 First i Position representation i Whether an uppercase letter appears。
23+
24+
Then we will mask1 and mask2 Perform and calculate,The results obtained mask First i Position representation i Whether the lethals of the letter appear at the same time。
25+
26+
As long as you get mask The highest level of binary representation 1 s position,Convert it to the corresponding capital letter。If all binary positions are not 1,Explain that there is no letter that appears at the same time,,Return to an empty string。
27+
28+
>author:ylb
29+
Link:https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/solutions/2077636/by-lcbin-zbg0/
30+
source:Deduction(LeetCode)
31+
著作权归author所有。商业转载请联系author获得授权,Non -commercial reprint Please indicate the source。
32+
33+
# Code:
34+
35+
```python Traversing the alphabet
36+
class Solution:
37+
def greatestLetter(self, s: str) -> str:
38+
for i in range(90, 64, -1):
39+
if chr(i) in s and chr(i+32) in s:
40+
return chr(i)
41+
return ""
42+
```
43+
```C++ Hash table
44+
class Solution {
45+
public:
46+
string greatestLetter(string s) {
47+
unordered_set<char> strin(s.begin(),s.end());
48+
for(char c = 'Z'; c >= 'A'; --c){
49+
if(strin.count(c) && strin.count(char(c+32))){
50+
return string(1,c);
51+
}
52+
}
53+
return "";
54+
}
55+
};
56+
```
57+
```python Bit operation
58+
class Solution:
59+
def greatestLetter(self, s: str) -> str:
60+
# We can use two integers mask1 and mask2
61+
# Record string separately s 中出现的小写字母and大写字母,in mask1 First i Position representation i Does a lowercase letter appear,
62+
# and mask2 First i Position representation i Whether an uppercase letter appears。
63+
mask1 = mask2 = 0
64+
for i in s:
65+
if i.islower():
66+
mask1 |= 1 << (ord(i) - ord("a"))
67+
else:
68+
mask2 |= 1 << (ord(i) - ord("A"))
69+
mask = mask1 & mask2
70+
return chr(mask.bit_length() - 1 + ord("A")) if mask else ""
71+
```
72+

0 commit comments

Comments
 (0)