Skip to content

Commit c1de100

Browse files
committed
feat: add solutions to lc problems: No.2309~2312
* No.2309.Greatest English Letter in Upper and Lower Case * No.2310.Sum of Numbers With Units Digit K * No.2311.Longest Binary Subsequence Less Than or Equal to K * No.2312.Selling Pieces of Wood
1 parent de35ad4 commit c1de100

File tree

32 files changed

+1892
-4
lines changed

32 files changed

+1892
-4
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# [2309. 兼具大小写的最好英文字母](https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case)
2+
3+
[English Version](/solution/2300-2399/2309.Greatest%20English%20Letter%20in%20Upper%20and%20Lower%20Case/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个由英文字母组成的字符串 <code>s</code> ,请你找出并返回 <code>s</code> 中的 <strong>最好</strong> 英文字母。返回的字母必须为大写形式。如果不存在满足条件的字母,则返回一个空字符串。</p>
10+
11+
<p><strong>最好</strong> 英文字母的大写和小写形式必须 <strong>都</strong> 在 <code>s</code> 中出现。</p>
12+
13+
<p>英文字母 <code>b</code> 比另一个英文字母&nbsp;<code>a</code>&nbsp;<strong>更好</strong> 的前提是:英文字母表中,<code>b</code> 在 <code>a</code> 之 <strong>后</strong> 出现。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>s = "l<em><strong>Ee</strong></em>TcOd<em><strong>E</strong></em>"
21+
<strong>输出:</strong>"E"
22+
<strong>解释:</strong>
23+
字母 'E' 是唯一一个大写和小写形式都出现的字母。</pre>
24+
25+
<p><strong>示例 2:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>s = "a<em><strong>rR</strong></em>AzFif"
29+
<strong>输出:</strong>"R"
30+
<strong>解释:</strong>
31+
字母 'R' 是大写和小写形式都出现的最好英文字母。
32+
注意 'A' 和 'F' 的大写和小写形式也都出现了,但是 'R' 比 'F' 和 'A' 更好。
33+
</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>s = "AbCdEfGhIjK"
39+
<strong>输出:</strong>""
40+
<strong>解释:</strong>
41+
不存在大写和小写形式都出现的字母。
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
50+
<li><code>s</code> 由小写和大写英文字母组成</li>
51+
</ul>
52+
53+
54+
## 解法
55+
56+
<!-- 这里可写通用的实现逻辑 -->
57+
58+
**方法一:哈希表 + 枚举**
59+
60+
<!-- tabs:start -->
61+
62+
### **Python3**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```python
67+
class Solution:
68+
def greatestLetter(self, s: str) -> str:
69+
ss = set(s)
70+
for c in ascii_uppercase[::-1]:
71+
if c in ss and c.lower() in ss:
72+
return c
73+
return ''
74+
```
75+
76+
### **Java**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```java
81+
class Solution {
82+
public String greatestLetter(String s) {
83+
int[] cnt = new int[26];
84+
for (char c : s.toCharArray()) {
85+
if (Character.isLowerCase(c)) {
86+
cnt[c - 'a'] |= 1;
87+
} else if (Character.isUpperCase(c)) {
88+
cnt[c - 'A'] |= 2;
89+
}
90+
}
91+
for (int i = 25; i >= 0; --i) {
92+
if (cnt[i] == 3) {
93+
return String.valueOf((char) ('A' + i));
94+
}
95+
}
96+
return "";
97+
}
98+
}
99+
```
100+
101+
```java
102+
class Solution {
103+
public String greatestLetter(String s) {
104+
Set<Character> ss = new HashSet<>();
105+
for (char c : s.toCharArray()) {
106+
ss.add(c);
107+
}
108+
for (char a = 'Z'; a >= 'A'; --a) {
109+
if (ss.contains(a) && ss.contains((char) (a + 32))) {
110+
return String.valueOf(a);
111+
}
112+
}
113+
return "";
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
string greatestLetter(string s) {
124+
unordered_set<char> ss;
125+
for (char& c : s) ss.insert(c);
126+
for (char c = 'Z'; c >= 'A'; --c)
127+
if (ss.count(c) && ss.count(char(c + 32)))
128+
return string(1, c);
129+
return "";
130+
}
131+
};
132+
```
133+
134+
### **Go**
135+
136+
```go
137+
func greatestLetter(s string) string {
138+
ss := map[rune]bool{}
139+
for _, c := range s {
140+
ss[c] = true
141+
}
142+
for c := 'Z'; c >= 'A'; c-- {
143+
if ss[c] && ss[rune(c+32)] {
144+
return string(c)
145+
}
146+
}
147+
return ""
148+
}
149+
```
150+
151+
### **TypeScript**
152+
153+
```ts
154+
155+
```
156+
157+
### **...**
158+
159+
```
160+
161+
```
162+
163+
<!-- tabs:end -->
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# [2309. Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case)
2+
3+
[中文文档](/solution/2300-2399/2309.Greatest%20English%20Letter%20in%20Upper%20and%20Lower%20Case/README.md)
4+
5+
## Description
6+
7+
<p>Given a string of English letters <code>s</code>, return <em>the <strong>greatest </strong>English letter which occurs as <strong>both</strong> a lowercase and uppercase letter in</em> <code>s</code>. The returned letter should be in <strong>uppercase</strong>. If no such letter exists, return <em>an empty string</em>.</p>
8+
9+
<p>An English letter <code>b</code> is <strong>greater</strong> than another letter <code>a</code> if <code>b</code> appears <strong>after</strong> <code>a</code> in the English alphabet.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> s = &quot;l<strong><u>Ee</u></strong>TcOd<u><strong>E</strong></u>&quot;
16+
<strong>Output:</strong> &quot;E&quot;
17+
<strong>Explanation:</strong>
18+
The letter &#39;E&#39; is the only letter to appear in both lower and upper case.
19+
</pre>
20+
21+
<p><strong>Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> s = &quot;a<strong><u>rR</u></strong>AzFif&quot;
25+
<strong>Output:</strong> &quot;R&quot;
26+
<strong>Explanation:</strong>
27+
The letter &#39;R&#39; is the greatest letter to appear in both lower and upper case.
28+
Note that &#39;A&#39; and &#39;F&#39; also appear in both lower and upper case, but &#39;R&#39; is greater than &#39;F&#39; or &#39;A&#39;.
29+
</pre>
30+
31+
<p><strong>Example 3:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> s = &quot;AbCdEfGhIjK&quot;
35+
<strong>Output:</strong> &quot;&quot;
36+
<strong>Explanation:</strong>
37+
There is no letter that appears in both lower and upper case.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
45+
<li><code>s</code> consists of lowercase and uppercase English letters.</li>
46+
</ul>
47+
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
class Solution:
57+
def greatestLetter(self, s: str) -> str:
58+
ss = set(s)
59+
for c in ascii_uppercase[::-1]:
60+
if c in ss and c.lower() in ss:
61+
return c
62+
return ''
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
class Solution {
69+
public String greatestLetter(String s) {
70+
int[] cnt = new int[26];
71+
for (char c : s.toCharArray()) {
72+
if (Character.isLowerCase(c)) {
73+
cnt[c - 'a'] |= 1;
74+
} else if (Character.isUpperCase(c)) {
75+
cnt[c - 'A'] |= 2;
76+
}
77+
}
78+
for (int i = 25; i >= 0; --i) {
79+
if (cnt[i] == 3) {
80+
return String.valueOf((char) ('A' + i));
81+
}
82+
}
83+
return "";
84+
}
85+
}
86+
```
87+
88+
```java
89+
class Solution {
90+
public String greatestLetter(String s) {
91+
Set<Character> ss = new HashSet<>();
92+
for (char c : s.toCharArray()) {
93+
ss.add(c);
94+
}
95+
for (char a = 'Z'; a >= 'A'; --a) {
96+
if (ss.contains(a) && ss.contains((char) (a + 32))) {
97+
return String.valueOf(a);
98+
}
99+
}
100+
return "";
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
string greatestLetter(string s) {
111+
unordered_set<char> ss;
112+
for (char& c : s) ss.insert(c);
113+
for (char c = 'Z'; c >= 'A'; --c)
114+
if (ss.count(c) && ss.count(char(c + 32)))
115+
return string(1, c);
116+
return "";
117+
}
118+
};
119+
```
120+
121+
### **Go**
122+
123+
```go
124+
func greatestLetter(s string) string {
125+
ss := map[rune]bool{}
126+
for _, c := range s {
127+
ss[c] = true
128+
}
129+
for c := 'Z'; c >= 'A'; c-- {
130+
if ss[c] && ss[rune(c+32)] {
131+
return string(c)
132+
}
133+
}
134+
return ""
135+
}
136+
```
137+
138+
### **TypeScript**
139+
140+
```ts
141+
142+
```
143+
144+
### **...**
145+
146+
```
147+
148+
```
149+
150+
<!-- tabs:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
string greatestLetter(string s) {
4+
unordered_set<char> ss;
5+
for (char& c : s) ss.insert(c);
6+
for (char c = 'Z'; c >= 'A'; --c)
7+
if (ss.count(c) && ss.count(char(c + 32)))
8+
return string(1, c);
9+
return "";
10+
}
11+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func greatestLetter(s string) string {
2+
ss := map[rune]bool{}
3+
for _, c := range s {
4+
ss[c] = true
5+
}
6+
for c := 'Z'; c >= 'A'; c-- {
7+
if ss[c] && ss[rune(c+32)] {
8+
return string(c)
9+
}
10+
}
11+
return ""
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public String greatestLetter(String s) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
if (Character.isLowerCase(c)) {
6+
cnt[c - 'a'] |= 1;
7+
} else if (Character.isUpperCase(c)) {
8+
cnt[c - 'A'] |= 2;
9+
}
10+
}
11+
for (int i = 25; i >= 0; --i) {
12+
if (cnt[i] == 3) {
13+
return String.valueOf((char) ('A' + i));
14+
}
15+
}
16+
return "";
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def greatestLetter(self, s: str) -> str:
3+
ss = set(s)
4+
for c in ascii_uppercase[::-1]:
5+
if c in ss and c.lower() in ss:
6+
return c
7+
return ''

0 commit comments

Comments
 (0)