Skip to content

Commit e77a392

Browse files
committed
feat: add solutions to lc problems: No.2455~2458
* No.2455.Average Value of Even Numbers That Are Divisible by Three * No.2456.Most Popular Video Creator * No.2457.Minimum Addition to Make Integer Beautiful * No.2458.Height of Binary Tree After Subtree Removal Queries
1 parent 64f4908 commit e77a392

File tree

37 files changed

+2421
-0
lines changed

37 files changed

+2421
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [2451. 差值数组不同的字符串](https://leetcode.cn/problems/odd-string-difference)
2+
3+
[English Version](/solution/2400-2499/2451.Odd%20String%20Difference/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个字符串数组 <code>words</code>&nbsp;,每一个字符串长度都相同,令所有字符串的长度都为 <code>n</code>&nbsp;。</p>
10+
11+
<p>每个字符串&nbsp;<code>words[i]</code>&nbsp;可以被转化为一个长度为&nbsp;<code>n - 1</code>&nbsp;&nbsp;<strong>差值整数数组</strong>&nbsp;<code>difference[i]</code>&nbsp;,其中对于&nbsp;<code>0 &lt;= j &lt;= n - 2</code>&nbsp;&nbsp;<code>difference[i][j] = words[i][j+1] - words[i][j]</code>&nbsp;。注意两个字母的差值定义为它们在字母表中&nbsp;<strong>位置</strong>&nbsp;之差,也就是说&nbsp;<code>'a'</code>&nbsp;的位置是&nbsp;<code>0</code>&nbsp;,<code>'b'</code>&nbsp;的位置是&nbsp;<code>1</code>&nbsp;,<code>'z'</code>&nbsp;的位置是&nbsp;<code>25</code>&nbsp;。</p>
12+
13+
<ul>
14+
<li>比方说,字符串&nbsp;<code>"acb"</code>&nbsp;的差值整数数组是&nbsp;<code>[2 - 0, 1 - 2] = [2, -1]</code>&nbsp;。</li>
15+
</ul>
16+
17+
<p><code>words</code>&nbsp;中所有字符串 <strong>除了一个字符串以外</strong>&nbsp;,其他字符串的差值整数数组都相同。你需要找到那个不同的字符串。</p>
18+
19+
<p>请你返回<em>&nbsp;</em><code>words</code>中&nbsp;<strong>差值整数数组</strong>&nbsp;不同的字符串。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>示例 1:</strong></p>
24+
25+
<pre>
26+
<b>输入:</b>words = ["adc","wzy","abc"]
27+
<b>输出:</b>"abc"
28+
<b>解释:</b>
29+
- "adc" 的差值整数数组是 [3 - 0, 2 - 3] = [3, -1] 。
30+
- "wzy" 的差值整数数组是 [25 - 22, 24 - 25]= [3, -1] 。
31+
- "abc" 的差值整数数组是 [1 - 0, 2 - 1] = [1, 1] 。
32+
不同的数组是 [1, 1],所以返回对应的字符串,"abc"。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre>
38+
<b>输入:</b>words = ["aaa","bob","ccc","ddd"]
39+
<b>输出:</b>"bob"
40+
<b>解释:</b>除了 "bob" 的差值整数数组是 [13, -13] 以外,其他字符串的差值整数数组都是 [0, 0] 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>3 &lt;= words.length &lt;= 100</code></li>
49+
<li><code>n == words[i].length</code></li>
50+
<li><code>2 &lt;= n &lt;= 20</code></li>
51+
<li><code>words[i]</code>&nbsp;只含有小写英文字母。</li>
52+
</ul>
53+
54+
## 解法
55+
56+
<!-- 这里可写通用的实现逻辑 -->
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```python
65+
66+
```
67+
68+
### **Java**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```java
73+
74+
```
75+
76+
### **TypeScript**
77+
78+
```ts
79+
80+
```
81+
82+
### **...**
83+
84+
```
85+
86+
```
87+
88+
<!-- tabs:end -->
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [2451. Odd String Difference](https://leetcode.com/problems/odd-string-difference)
2+
3+
[中文文档](/solution/2400-2499/2451.Odd%20String%20Difference/README.md)
4+
5+
## Description
6+
7+
<p>You are given an array of equal-length strings <code>words</code>. Assume that the length of each string is <code>n</code>.</p>
8+
9+
<p>Each string <code>words[i]</code> can be converted into a <strong>difference integer array</strong> <code>difference[i]</code> of length <code>n - 1</code> where <code>difference[i][j] = words[i][j+1] - words[i][j]</code> where <code>0 &lt;= j &lt;= n - 2</code>. Note that the difference between two letters is the difference between their <strong>positions</strong> in the alphabet i.e.&nbsp;the position of <code>&#39;a&#39;</code> is <code>0</code>, <code>&#39;b&#39;</code> is <code>1</code>, and <code>&#39;z&#39;</code> is <code>25</code>.</p>
10+
11+
<ul>
12+
<li>For example, for the string <code>&quot;acb&quot;</code>, the difference integer array is <code>[2 - 0, 1 - 2] = [2, -1]</code>.</li>
13+
</ul>
14+
15+
<p>All the strings in words have the same difference integer array, <strong>except one</strong>. You should find that string.</p>
16+
17+
<p>Return<em> the string in </em><code>words</code><em> that has different <strong>difference integer array</strong>.</em></p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> words = [&quot;adc&quot;,&quot;wzy&quot;,&quot;abc&quot;]
24+
<strong>Output:</strong> &quot;abc&quot;
25+
<strong>Explanation:</strong>
26+
- The difference integer array of &quot;adc&quot; is [3 - 0, 2 - 3] = [3, -1].
27+
- The difference integer array of &quot;wzy&quot; is [25 - 22, 24 - 25]= [3, -1].
28+
- The difference integer array of &quot;abc&quot; is [1 - 0, 2 - 1] = [1, 1].
29+
The odd array out is [1, 1], so we return the corresponding string, &quot;abc&quot;.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> words = [&quot;aaa&quot;,&quot;bob&quot;,&quot;ccc&quot;,&quot;ddd&quot;]
36+
<strong>Output:</strong> &quot;bob&quot;
37+
<strong>Explanation:</strong> All the integer arrays are [0, 0] except for &quot;bob&quot;, which corresponds to [13, -13].
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>3 &lt;= words.length &lt;= 100</code></li>
45+
<li><code>n == words[i].length</code></li>
46+
<li><code>2 &lt;= n &lt;= 20</code></li>
47+
<li><code>words[i]</code> consists of lowercase English letters.</li>
48+
</ul>
49+
50+
## Solutions
51+
52+
<!-- tabs:start -->
53+
54+
### **Python3**
55+
56+
```python
57+
58+
```
59+
60+
### **Java**
61+
62+
```java
63+
64+
```
65+
66+
### **TypeScript**
67+
68+
```ts
69+
70+
```
71+
72+
### **...**
73+
74+
```
75+
76+
```
77+
78+
<!-- tabs:end -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2452. 距离字典两次编辑以内的单词](https://leetcode.cn/problems/words-within-two-edits-of-dictionary)
2+
3+
[English Version](/solution/2400-2499/2452.Words%20Within%20Two%20Edits%20of%20Dictionary/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你两个字符串数组&nbsp;<code>queries</code> 和&nbsp;<code>dictionary</code>&nbsp;。数组中所有单词都只包含小写英文字母,且长度都相同。</p>
10+
11+
<p>一次 <strong>编辑</strong>&nbsp;中,你可以从 <code>queries</code>&nbsp;中选择一个单词,将任意一个字母修改成任何其他字母。从&nbsp;<code>queries</code>&nbsp;中找到所有满足以下条件的字符串:<strong>不超过</strong>&nbsp;两次编辑内,字符串与&nbsp;<code>dictionary</code>&nbsp;中某个字符串相同。</p>
12+
13+
<p>请你返回<em>&nbsp;</em><code>queries</code>&nbsp;中的单词列表,这些单词距离&nbsp;<code>dictionary</code>&nbsp;中的单词&nbsp;<strong>编辑次数</strong>&nbsp;不超过&nbsp;<strong>两次</strong>&nbsp;。单词返回的顺序需要与&nbsp;<code>queries</code>&nbsp;中原本顺序相同。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre><b>输入:</b>queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]
20+
<b>输出:</b>["word","note","wood"]
21+
<strong>解释:</strong>
22+
- 将 "word" 中的 'r' 换成 'o' ,得到 dictionary 中的单词 "wood" 。
23+
- 将 "note" 中的 'n' 换成 'j' 且将 't' 换成 'k' ,得到 "joke" 。
24+
- "ants" 需要超过 2 次编辑才能得到 dictionary 中的单词。
25+
- "wood" 不需要修改(0 次编辑),就得到 dictionary 中相同的单词。
26+
所以我们返回 ["word","note","wood"] 。
27+
</pre>
28+
29+
<p><strong>示例 2:</strong></p>
30+
31+
<pre><b>输入:</b>queries = ["yes"], dictionary = ["not"]
32+
<b>输出:</b>[]
33+
<strong>解释:</strong>
34+
"yes" 需要超过 2 次编辑才能得到 "not" 。
35+
所以我们返回空数组。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= queries.length, dictionary.length &lt;= 100</code></li>
44+
<li><code>n == queries[i].length == dictionary[j].length</code></li>
45+
<li><code>1 &lt;= n &lt;= 100</code></li>
46+
<li>所有&nbsp;<code>queries[i]</code> 和&nbsp;<code>dictionary[j]</code>&nbsp;都只包含小写英文字母。</li>
47+
</ul>
48+
49+
## 解法
50+
51+
<!-- 这里可写通用的实现逻辑 -->
52+
53+
<!-- tabs:start -->
54+
55+
### **Python3**
56+
57+
<!-- 这里可写当前语言的特殊实现逻辑 -->
58+
59+
```python
60+
61+
```
62+
63+
### **Java**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```java
68+
69+
```
70+
71+
### **TypeScript**
72+
73+
```ts
74+
75+
```
76+
77+
### **...**
78+
79+
```
80+
81+
```
82+
83+
<!-- tabs:end -->
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [2452. Words Within Two Edits of Dictionary](https://leetcode.com/problems/words-within-two-edits-of-dictionary)
2+
3+
[中文文档](/solution/2400-2499/2452.Words%20Within%20Two%20Edits%20of%20Dictionary/README.md)
4+
5+
## Description
6+
7+
<p>You are given two string arrays, <code>queries</code> and <code>dictionary</code>. All words in each array comprise of lowercase English letters and have the same length.</p>
8+
9+
<p>In one <strong>edit</strong> you can take a word from <code>queries</code>, and change any letter in it to any other letter. Find all words from <code>queries</code> that, after a <strong>maximum</strong> of two edits, equal some word from <code>dictionary</code>.</p>
10+
11+
<p>Return<em> a list of all words from </em><code>queries</code><em>, </em><em>that match with some word from </em><code>dictionary</code><em> after a maximum of <strong>two edits</strong></em>. Return the words in the <strong>same order</strong> they appear in <code>queries</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> queries = [&quot;word&quot;,&quot;note&quot;,&quot;ants&quot;,&quot;wood&quot;], dictionary = [&quot;wood&quot;,&quot;joke&quot;,&quot;moat&quot;]
18+
<strong>Output:</strong> [&quot;word&quot;,&quot;note&quot;,&quot;wood&quot;]
19+
<strong>Explanation:</strong>
20+
- Changing the &#39;r&#39; in &quot;word&quot; to &#39;o&#39; allows it to equal the dictionary word &quot;wood&quot;.
21+
- Changing the &#39;n&#39; to &#39;j&#39; and the &#39;t&#39; to &#39;k&#39; in &quot;note&quot; changes it to &quot;joke&quot;.
22+
- It would take more than 2 edits for &quot;ants&quot; to equal a dictionary word.
23+
- &quot;wood&quot; can remain unchanged (0 edits) and match the corresponding dictionary word.
24+
Thus, we return [&quot;word&quot;,&quot;note&quot;,&quot;wood&quot;].
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> queries = [&quot;yes&quot;], dictionary = [&quot;not&quot;]
31+
<strong>Output:</strong> []
32+
<strong>Explanation:</strong>
33+
Applying any two edits to &quot;yes&quot; cannot make it equal to &quot;not&quot;. Thus, we return an empty array.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= queries.length, dictionary.length &lt;= 100</code></li>
41+
<li><code>n == queries[i].length == dictionary[j].length</code></li>
42+
<li><code>1 &lt;= n &lt;= 100</code></li>
43+
<li>All <code>queries[i]</code> and <code>dictionary[j]</code> are composed of lowercase English letters.</li>
44+
</ul>
45+
46+
## Solutions
47+
48+
<!-- tabs:start -->
49+
50+
### **Python3**
51+
52+
```python
53+
54+
```
55+
56+
### **Java**
57+
58+
```java
59+
60+
```
61+
62+
### **TypeScript**
63+
64+
```ts
65+
66+
```
67+
68+
### **...**
69+
70+
```
71+
72+
```
73+
74+
<!-- tabs:end -->

0 commit comments

Comments
 (0)