Skip to content

Commit a88a827

Browse files
authored
feat: add solutions to lc problem: No.2452 (doocs#2778)
No.2452.Words Within Two Edits of Dictionary
1 parent 9aed236 commit a88a827

File tree

12 files changed

+163
-74
lines changed

12 files changed

+163
-74
lines changed

solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ Note that even though the subarrays have the same content, the two subarrays are
4848

4949
## Solutions
5050

51-
### Solution 1
51+
### Solution 1: Hash Table
52+
53+
We can traverse the array $nums$, and use a hash table $vis$ to record the sum of every two adjacent elements in the array. If the sum of the current two elements has already appeared in the hash table, then return `true`. Otherwise, add the sum of the current two elements to the hash table.
54+
55+
If we finish traversing and haven't found two subarrays that meet the condition, return `false`.
56+
57+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
5258

5359
<!-- tabs:start -->
5460

solution/2300-2399/2396.Strictly Palindromic Number/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646

4747
### 方法一:脑筋急转弯
4848

49-
当 $n=4$ 时,二进制表示为 $100$,不是回文串;
49+
当 $n = 4$ 时,二进制表示为 $100$,不是回文串;
5050

51-
当 $n \gt 4$ 时,此时 $n-2$ 的二进制表示为 $12$,不是回文串。
51+
当 $n \gt 4$ 时,此时 $n - 2$ 进制表示为 $12$,不是回文串。
5252

53-
因此,我们直接返回 `false` 即可
53+
因此,我们可以直接返回 `false`
5454

5555
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5656

solution/2300-2399/2396.Strictly Palindromic Number/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ Therefore, we return false.
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Quick Thinking
47+
48+
When $n = 4$, its binary representation is $100$, which is not a palindrome;
49+
50+
When $n \gt 4$, its $(n - 2)$-ary representation is $12$, which is not a palindrome.
51+
52+
Therefore, we can directly return `false`.
53+
54+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
4755

4856
<!-- tabs:start -->
4957

solution/2300-2399/2398.Maximum Number of Robots Within Budget/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050

5151
### 方法一:双指针 + 单调队列
5252

53-
问题实际上是求滑动窗口内的最大值,可以用单调队列来求解。只需要二分枚举窗口 $k$ 的大小,找到一个最大的 $k$,使得满足题目要求。
53+
问题实际上是求滑动窗口内的最大值,可以用单调队列来求解。
54+
55+
我们只需要二分枚举窗口 $k$ 的大小,找到一个最大的 $k$,使得满足题目要求。
5456

5557
实现过程中,实际上不需要进行二分枚举,只需要将固定窗口改为双指针非固定窗口即可。
5658

solution/2300-2399/2398.Maximum Number of Robots Within Budget/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ It can be shown that it is not possible to run more than 3 consecutive robots wi
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Two Pointers + Monotonic Queue
48+
49+
The problem is essentially finding the maximum value within a sliding window, which can be solved using a monotonic queue.
50+
51+
We only need to use binary search to enumerate the size of the window $k$, and find the largest $k$ that satisfies the problem requirements.
52+
53+
In the implementation process, we don't actually need to perform binary search enumeration. We just need to change the fixed window to a non-fixed window with double pointers.
54+
55+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of robots in the problem.
4856

4957
<!-- tabs:start -->
5058

solution/2400-2499/2417.Closest Fair Integer/README_EN.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@
3939

4040
## Solutions
4141

42-
### Solution 1
42+
### Solution 1: Case Discussion
43+
44+
We denote the number of digits of $n$ as $k$, and the number of odd and even digits as $a$ and $b$ respectively.
45+
46+
- If $a = b$, then $n$ itself is `fair`, and we can directly return $n$;
47+
- Otherwise, if $k$ is odd, we can find the smallest `fair` number with $k+1$ digits, in the form of `10000111`. If $k$ is even, we can directly brute force `closestFair(n+1)`.
48+
49+
The time complexity is $O(\sqrt{n} \times \log_{10} n)$.
4350

4451
<!-- tabs:start -->
4552

solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md

+42-23
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252

5353
### 方法一:暴力枚举
5454

55-
遍历 `queries` 中的每个单词,对于每个单词,遍历 `dictionary` 中的每个单词,判断两个单词不同字符的位置数是否小于 $3$,如果是,则将该单词加入结果集
55+
我们直接遍历数组 $\text{queries}$ 中的每个单词 $s$,再遍历数组 $\text{dictionary}$ 中的每个单词 $t$,如果存在一个单词 $t$ 与 $s$ 的编辑距离小于 $3$,则将 $s$ 加入答案数组中,然后退出内层循环的遍历。如果不存在这样的单词 $t$,则继续遍历下一个单词 $s$
5656

57-
时间复杂度 $O(m\times n\times k)$。其中 $m$ 和 $n$ 分别是 `queries``dictionary` 的长度,而 $k$ 是 `queries``dictionary` 中单词的长度
57+
时间复杂度 $O(m \times n \times l)$,其中 $m$ 和 $n$ 分别是数组 $\text{queries}$$\text{dictionary}$ 的长度,而 $l$ 是单词的长度
5858

5959
<!-- tabs:start -->
6060

@@ -102,7 +102,9 @@ public:
102102
for (auto& s : queries) {
103103
for (auto& t : dictionary) {
104104
int cnt = 0;
105-
for (int i = 0; i < s.size(); ++i) cnt += s[i] != t[i];
105+
for (int i = 0; i < s.size(); ++i) {
106+
cnt += s[i] != t[i];
107+
}
106108
if (cnt < 3) {
107109
ans.emplace_back(s);
108110
break;
@@ -137,15 +139,15 @@ func twoEditWords(queries []string, dictionary []string) (ans []string) {
137139
```ts
138140
function twoEditWords(queries: string[], dictionary: string[]): string[] {
139141
const n = queries[0].length;
140-
return queries.filter(querie => {
141-
for (const s of dictionary) {
142+
return queries.filter(s => {
143+
for (const t of dictionary) {
142144
let diff = 0;
143-
for (let i = 0; i < n; i++) {
144-
if (querie[i] !== s[i] && ++diff > 2) {
145-
break;
145+
for (let i = 0; i < n; ++i) {
146+
if (s[i] !== t[i]) {
147+
++diff;
146148
}
147149
}
148-
if (diff <= 2) {
150+
if (diff < 3) {
149151
return true;
150152
}
151153
}
@@ -157,28 +159,45 @@ function twoEditWords(queries: string[], dictionary: string[]): string[] {
157159
```rust
158160
impl Solution {
159161
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
160-
let n = queries[0].len();
161162
queries
162163
.into_iter()
163-
.filter(|querie| {
164-
for s in dictionary.iter() {
165-
let mut diff = 0;
166-
for i in 0..n {
167-
if querie.as_bytes()[i] != s.as_bytes()[i] {
168-
diff += 1;
169-
}
170-
}
171-
if diff <= 2 {
172-
return true;
173-
}
174-
}
175-
false
164+
.filter(|s| {
165+
dictionary.iter().any(|t| {
166+
s
167+
.chars()
168+
.zip(t.chars())
169+
.filter(|&(a, b)| a != b)
170+
.count() < 3
171+
})
176172
})
177173
.collect()
178174
}
179175
}
180176
```
181177

178+
```cs
179+
public class Solution {
180+
public IList<string> TwoEditWords(string[] queries, string[] dictionary) {
181+
var ans = new List<string>();
182+
foreach (var s in queries) {
183+
foreach (var t in dictionary) {
184+
int cnt = 0;
185+
for (int i = 0; i < s.Length; i++) {
186+
if (s[i] != t[i]) {
187+
cnt++;
188+
}
189+
}
190+
if (cnt < 3) {
191+
ans.Add(s);
192+
break;
193+
}
194+
}
195+
}
196+
return ans;
197+
}
198+
}
199+
```
200+
182201
<!-- tabs:end -->
183202

184203
<!-- end -->

solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md

+45-22
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ Applying any two edits to &quot;yes&quot; cannot make it equal to &quot;not&quot
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Brute Force Enumeration
51+
52+
We directly traverse each word $s$ in the array $\text{queries}$, and then traverse each word $t$ in the array $\text{dictionary}$. If there exists a word $t$ whose edit distance from $s$ is less than $3$, we add $s$ to the answer array and then exit the inner loop. If there is no such word $t$, we continue to traverse the next word $s$.
53+
54+
The time complexity is $O(m \times n \times l)$, where $m$ and $n$ are the lengths of the arrays $\text{queries}$ and $\text{dictionary}$ respectively, and $l$ is the length of the word.
5155

5256
<!-- tabs:start -->
5357

@@ -95,7 +99,9 @@ public:
9599
for (auto& s : queries) {
96100
for (auto& t : dictionary) {
97101
int cnt = 0;
98-
for (int i = 0; i < s.size(); ++i) cnt += s[i] != t[i];
102+
for (int i = 0; i < s.size(); ++i) {
103+
cnt += s[i] != t[i];
104+
}
99105
if (cnt < 3) {
100106
ans.emplace_back(s);
101107
break;
@@ -130,15 +136,15 @@ func twoEditWords(queries []string, dictionary []string) (ans []string) {
130136
```ts
131137
function twoEditWords(queries: string[], dictionary: string[]): string[] {
132138
const n = queries[0].length;
133-
return queries.filter(querie => {
134-
for (const s of dictionary) {
139+
return queries.filter(s => {
140+
for (const t of dictionary) {
135141
let diff = 0;
136-
for (let i = 0; i < n; i++) {
137-
if (querie[i] !== s[i] && ++diff > 2) {
138-
break;
142+
for (let i = 0; i < n; ++i) {
143+
if (s[i] !== t[i]) {
144+
++diff;
139145
}
140146
}
141-
if (diff <= 2) {
147+
if (diff < 3) {
142148
return true;
143149
}
144150
}
@@ -150,28 +156,45 @@ function twoEditWords(queries: string[], dictionary: string[]): string[] {
150156
```rust
151157
impl Solution {
152158
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
153-
let n = queries[0].len();
154159
queries
155160
.into_iter()
156-
.filter(|querie| {
157-
for s in dictionary.iter() {
158-
let mut diff = 0;
159-
for i in 0..n {
160-
if querie.as_bytes()[i] != s.as_bytes()[i] {
161-
diff += 1;
162-
}
163-
}
164-
if diff <= 2 {
165-
return true;
166-
}
167-
}
168-
false
161+
.filter(|s| {
162+
dictionary.iter().any(|t| {
163+
s
164+
.chars()
165+
.zip(t.chars())
166+
.filter(|&(a, b)| a != b)
167+
.count() < 3
168+
})
169169
})
170170
.collect()
171171
}
172172
}
173173
```
174174

175+
```cs
176+
public class Solution {
177+
public IList<string> TwoEditWords(string[] queries, string[] dictionary) {
178+
var ans = new List<string>();
179+
foreach (var s in queries) {
180+
foreach (var t in dictionary) {
181+
int cnt = 0;
182+
for (int i = 0; i < s.Length; i++) {
183+
if (s[i] != t[i]) {
184+
cnt++;
185+
}
186+
}
187+
if (cnt < 3) {
188+
ans.Add(s);
189+
break;
190+
}
191+
}
192+
}
193+
return ans;
194+
}
195+
}
196+
```
197+
175198
<!-- tabs:end -->
176199

177200
<!-- end -->

solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ class Solution {
55
for (auto& s : queries) {
66
for (auto& t : dictionary) {
77
int cnt = 0;
8-
for (int i = 0; i < s.size(); ++i) cnt += s[i] != t[i];
8+
for (int i = 0; i < s.size(); ++i) {
9+
cnt += s[i] != t[i];
10+
}
911
if (cnt < 3) {
1012
ans.emplace_back(s);
1113
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public IList<string> TwoEditWords(string[] queries, string[] dictionary) {
3+
var ans = new List<string>();
4+
foreach (var s in queries) {
5+
foreach (var t in dictionary) {
6+
int cnt = 0;
7+
for (int i = 0; i < s.Length; i++) {
8+
if (s[i] != t[i]) {
9+
cnt++;
10+
}
11+
}
12+
if (cnt < 3) {
13+
ans.Add(s);
14+
break;
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}

solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
impl Solution {
22
pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
3-
let n = queries[0].len();
43
queries
54
.into_iter()
6-
.filter(|querie| {
7-
for s in dictionary.iter() {
8-
let mut diff = 0;
9-
for i in 0..n {
10-
if querie.as_bytes()[i] != s.as_bytes()[i] {
11-
diff += 1;
12-
}
13-
}
14-
if diff <= 2 {
15-
return true;
16-
}
17-
}
18-
false
5+
.filter(|s| {
6+
dictionary.iter().any(|t| {
7+
s
8+
.chars()
9+
.zip(t.chars())
10+
.filter(|&(a, b)| a != b)
11+
.count() < 3
12+
})
1913
})
2014
.collect()
2115
}

solution/2400-2499/2452.Words Within Two Edits of Dictionary/Solution.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
function twoEditWords(queries: string[], dictionary: string[]): string[] {
22
const n = queries[0].length;
3-
return queries.filter(querie => {
4-
for (const s of dictionary) {
3+
return queries.filter(s => {
4+
for (const t of dictionary) {
55
let diff = 0;
6-
for (let i = 0; i < n; i++) {
7-
if (querie[i] !== s[i] && ++diff > 2) {
8-
break;
6+
for (let i = 0; i < n; ++i) {
7+
if (s[i] !== t[i]) {
8+
++diff;
99
}
1010
}
11-
if (diff <= 2) {
11+
if (diff < 3) {
1212
return true;
1313
}
1414
}

0 commit comments

Comments
 (0)