Skip to content

Commit e667710

Browse files
authored
chore: add new lc problems (#1066)
1 parent f159332 commit e667710

File tree

14 files changed

+781
-0
lines changed

14 files changed

+781
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# [2744. 最大字符串配对数目](https://leetcode.cn/problems/find-maximum-number-of-string-pairs)
2+
3+
[English Version](/solution/2700-2799/2744.Find%20Maximum%20Number%20of%20String%20Pairs/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>words</code>&nbsp;,数组中包含 <strong>互不相同</strong>&nbsp;的字符串。</p>
10+
11+
<p>如果字符串&nbsp;<code>words[i]</code>&nbsp;与字符串 <code>words[j]</code>&nbsp;满足以下条件,我们称它们可以匹配:</p>
12+
13+
<ul>
14+
<li>字符串&nbsp;<code>words[i]</code>&nbsp;等于&nbsp;<code>words[j]</code>&nbsp;的反转字符串。</li>
15+
<li><code>0 &lt;= i &lt; j &lt; words.length</code></li>
16+
</ul>
17+
18+
<p>请你返回数组 <code>words</code>&nbsp;中的&nbsp;<strong>最大</strong>&nbsp;匹配数目。</p>
19+
20+
<p>注意,每个字符串最多匹配一次。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong>示例 1:</strong></p>
25+
26+
<pre>
27+
<b>输入:</b>words = ["cd","ac","dc","ca","zz"]
28+
<b>输出:</b>2
29+
<strong>解释:</strong>在此示例中,我们可以通过以下方式匹配 2 对字符串:
30+
- 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 "dc" 并且等于 words[2]。
31+
- 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 "ca" 并且等于 words[3]。
32+
可以证明最多匹配数目是 2 。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre>
38+
<b>输入:</b>words = ["ab","ba","cc"]
39+
<b>输出:</b>1
40+
<b>解释:</b>在此示例中,我们可以通过以下方式匹配 1 对字符串:
41+
- 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 "ab" 与 words[0] 相等。
42+
可以证明最多匹配数目是 1 。
43+
</pre>
44+
45+
<p><strong>示例 3:</strong></p>
46+
47+
<pre>
48+
<b>输入:</b>words = ["aa","ab"]
49+
<b>输出:</b>0
50+
<strong>解释:</strong>这个例子中,无法匹配任何字符串。
51+
</pre>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= words.length &lt;= 50</code></li>
59+
<li><code>words[i].length == 2</code></li>
60+
<li><code>words</code>&nbsp;包含的字符串互不相同。</li>
61+
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>
62+
</ul>
63+
64+
## 解法
65+
66+
<!-- 这里可写通用的实现逻辑 -->
67+
68+
<!-- tabs:start -->
69+
70+
### **Python3**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```python
75+
76+
```
77+
78+
### **Java**
79+
80+
<!-- 这里可写当前语言的特殊实现逻辑 -->
81+
82+
```java
83+
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
90+
```
91+
92+
### **Go**
93+
94+
```go
95+
96+
```
97+
98+
### **...**
99+
100+
```
101+
102+
```
103+
104+
<!-- tabs:end -->
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2744. Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs)
2+
3+
[中文文档](/solution/2700-2799/2744.Find%20Maximum%20Number%20of%20String%20Pairs/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array <code>words</code> consisting of <strong>distinct</strong> strings.</p>
8+
9+
<p>The string <code>words[i]</code> can be paired with the string <code>words[j]</code> if:</p>
10+
11+
<ul>
12+
<li>The string <code>words[i]</code> is equal to the reversed string of <code>words[j]</code>.</li>
13+
<li><code>0 &lt;= i &lt; j &lt; words.length</code>.</li>
14+
</ul>
15+
16+
<p>Return <em>the <strong>maximum</strong> number of pairs that can be formed from the array </em><code>words</code><em>.</em></p>
17+
18+
<p>Note that&nbsp;each string can belong in&nbsp;<strong>at most one</strong> pair.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> words = [&quot;cd&quot;,&quot;ac&quot;,&quot;dc&quot;,&quot;ca&quot;,&quot;zz&quot;]
25+
<strong>Output:</strong> 2
26+
<strong>Explanation:</strong> In this example, we can form 2 pair of strings in the following way:
27+
- We pair the 0<sup>th</sup> string with the 2<sup>nd</sup> string, as the reversed string of word[0] is &quot;dc&quot; and is equal to words[2].
28+
- We pair the 1<sup>st</sup> string with the 3<sup>rd</sup> string, as the reversed string of word[1] is &quot;ca&quot; and is equal to words[3].
29+
It can be proven that 2 is the maximum number of pairs that can be formed.</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> words = [&quot;ab&quot;,&quot;ba&quot;,&quot;cc&quot;]
35+
<strong>Output:</strong> 1
36+
<strong>Explanation:</strong> In this example, we can form 1 pair of strings in the following way:
37+
- We pair the 0<sup>th</sup> string with the 1<sup>st</sup> string, as the reversed string of words[1] is &quot;ab&quot; and is equal to words[0].
38+
It can be proven that 1 is the maximum number of pairs that can be formed.
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> words = [&quot;aa&quot;,&quot;ab&quot;]
45+
<strong>Output:</strong> 0
46+
<strong>Explanation:</strong> In this example, we are unable to form any pair of strings.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= words.length &lt;= 50</code></li>
54+
<li><code>words[i].length == 2</code></li>
55+
<li><code>words</code>&nbsp;consists of distinct strings.</li>
56+
<li><code>words[i]</code>&nbsp;contains only lowercase English letters.</li>
57+
</ul>
58+
59+
## Solutions
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
```python
66+
67+
```
68+
69+
### **Java**
70+
71+
```java
72+
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
79+
```
80+
81+
### **Go**
82+
83+
```go
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [2745. 构造最长的新字符串](https://leetcode.cn/problems/construct-the-longest-new-string)
2+
3+
[English Version](/solution/2700-2799/2745.Construct%20the%20Longest%20New%20String/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你三个整数&nbsp;<code>x</code>&nbsp;,<code>y</code>&nbsp;&nbsp;<code>z</code>&nbsp;。</p>
10+
11+
<p>这三个整数表示你有&nbsp;<code>x</code>&nbsp;&nbsp;<code>"AA"</code>&nbsp;字符串,<code>y</code>&nbsp;&nbsp;<code>"BB"</code>&nbsp;字符串,和&nbsp;<code>z</code>&nbsp;&nbsp;<code>"AB"</code>&nbsp;字符串。你需要选择这些字符串中的部分字符串(可以全部选择也可以一个都不选择),将它们按顺序连接得到一个新的字符串。新字符串不能包含子字符串&nbsp;<code>"AAA"</code>&nbsp;或者&nbsp;<code>"BBB"</code>&nbsp;。</p>
12+
13+
<p>请你返回新字符串的最大可能长度。</p>
14+
15+
<p><strong>子字符串</strong>&nbsp;是一个字符串中一段连续 <strong>非空</strong>&nbsp;的字符序列。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
21+
<pre>
22+
<b>输入:</b>x = 2, y = 5, z = 1
23+
<b>输出:</b>12
24+
<strong>解释: </strong>我们可以按顺序连接 "BB" ,"AA" ,"BB" ,"AA" ,"BB" 和 "AB" ,得到新字符串 "BBAABBAABBAB" 。
25+
字符串长度为 12 ,无法得到一个更长的符合题目要求的字符串。
26+
</pre>
27+
28+
<p><strong class="example">示例 2:</strong></p>
29+
30+
<pre>
31+
<b>输入:</b>x = 3, y = 2, z = 2
32+
<b>输出:</b>14
33+
<b>解释:</b>我们可以按顺序连接 "AB" ,"AB" ,"AA" ,"BB" ,"AA" ,"BB" 和 "AA" ,得到新字符串 "ABABAABBAABBAA" 。
34+
字符串长度为 14 ,无法得到一个更长的符合题目要求的字符串。
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= x, y, z &lt;= 50</code></li>
43+
</ul>
44+
45+
## 解法
46+
47+
<!-- 这里可写通用的实现逻辑 -->
48+
49+
<!-- tabs:start -->
50+
51+
### **Python3**
52+
53+
<!-- 这里可写当前语言的特殊实现逻辑 -->
54+
55+
```python
56+
57+
```
58+
59+
### **Java**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```java
64+
65+
```
66+
67+
### **C++**
68+
69+
```cpp
70+
71+
```
72+
73+
### **Go**
74+
75+
```go
76+
77+
```
78+
79+
### **...**
80+
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [2745. Construct the Longest New String](https://leetcode.com/problems/construct-the-longest-new-string)
2+
3+
[中文文档](/solution/2700-2799/2745.Construct%20the%20Longest%20New%20String/README.md)
4+
5+
## Description
6+
7+
<p>You are given three integers <code>x</code>, <code>y</code>, and <code>z</code>.</p>
8+
9+
<p>You have <code>x</code> strings equal to <code>&quot;AA&quot;</code>, <code>y</code> strings equal to <code>&quot;BB&quot;</code>, and <code>z</code> strings equal to <code>&quot;AB&quot;</code>. You want to choose some (possibly all or none) of these strings and concactenate them in some order to form a new string. This new string must not contain <code>&quot;AAA&quot;</code> or <code>&quot;BBB&quot;</code> as a substring.</p>
10+
11+
<p>Return <em>the maximum possible length of the new string</em>.</p>
12+
13+
<p>A <b>substring</b> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> x = 2, y = 5, z = 1
20+
<strong>Output:</strong> 12
21+
<strong>Explanation: </strong>We can concactenate the strings &quot;BB&quot;, &quot;AA&quot;, &quot;BB&quot;, &quot;AA&quot;, &quot;BB&quot;, and &quot;AB&quot; in that order. Then, our new string is &quot;BBAABBAABBAB&quot;.
22+
That string has length 12, and we can show that it is impossible to construct a string of longer length.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> x = 3, y = 2, z = 2
29+
<strong>Output:</strong> 14
30+
<strong>Explanation:</strong> We can concactenate the strings &quot;AB&quot;, &quot;AB&quot;, &quot;AA&quot;, &quot;BB&quot;, &quot;AA&quot;, &quot;BB&quot;, and &quot;AA&quot; in that order. Then, our new string is &quot;ABABAABBAABBAA&quot;.
31+
That string has length 14, and we can show that it is impossible to construct a string of longer length.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= x, y, z &lt;= 50</code></li>
39+
</ul>
40+
41+
## Solutions
42+
43+
<!-- tabs:start -->
44+
45+
### **Python3**
46+
47+
```python
48+
49+
```
50+
51+
### **Java**
52+
53+
```java
54+
55+
```
56+
57+
### **C++**
58+
59+
```cpp
60+
61+
```
62+
63+
### **Go**
64+
65+
```go
66+
67+
```
68+
69+
### **...**
70+
71+
```
72+
73+
```
74+
75+
<!-- tabs:end -->

0 commit comments

Comments
 (0)