Skip to content

Commit 2c6dc1c

Browse files
update: 添加问题“2452.距离字典两次编辑以内的单词”的代码和题解 (#1537)
* 2452: WA.cpp (#1535) - forgot to break * 2452: AC.cpp (#1535) -AC,43.18%,29.55% * update: 添加问题“2452.距离字典两次编辑以内的单词”的代码和题解 (#1537) Signed-off-by: LetMeFly666 <Tisfy@qq.com> * typo: ¥->$ Update Solutions/LeetCode 2452.距离字典两次编辑以内的单词.md Co-authored-by: gh-pr-review[bot] <268385713+gh-pr-review[bot]@users.noreply.github.com> * typo: remove多余汉字 Update Solutions/LeetCode 2452.距离字典两次编辑以内的单词.md Co-authored-by: gh-pr-review[bot] <268385713+gh-pr-review[bot]@users.noreply.github.com> --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com> Co-authored-by: gh-pr-review[bot] <268385713+gh-pr-review[bot]@users.noreply.github.com>
1 parent 19cbfc9 commit 2c6dc1c

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-04-22 23:07:22
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-04-22 23:10:53
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
inline bool ok(const string& a, const string& b) {
13+
int diff = 0;
14+
for (int i = 0; i < a.size(); i++) {
15+
diff += a[i] != b[i];
16+
}
17+
return diff <= 2;
18+
}
19+
public:
20+
vector<string> twoEditWords(vector<string>& queries, vector<string>& dictionary) {
21+
vector<string> ans;
22+
for (string& a : queries) {
23+
for (string& b : dictionary) {
24+
if (ok(a, b)) {
25+
ans.push_back(a);
26+
break;
27+
}
28+
}
29+
}
30+
return ans;
31+
}
32+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@
863863
|2441.与对应负数同时存在的最大正整数|简单|<a href="https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/13/LeetCode%202441.%E4%B8%8E%E5%AF%B9%E5%BA%94%E8%B4%9F%E6%95%B0%E5%90%8C%E6%97%B6%E5%AD%98%E5%9C%A8%E7%9A%84%E6%9C%80%E5%A4%A7%E6%AD%A3%E6%95%B4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130656433" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/solutions/2268118/letmefly-2441yu-dui-ying-fu-shu-tong-shi-9c8j/" target="_blank">LeetCode题解</a>|
864864
|2446.判断两个事件是否存在冲突|简单|<a href="https://leetcode.cn/problems/determine-if-two-events-have-conflict/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/17/LeetCode%202446.%E5%88%A4%E6%96%AD%E4%B8%A4%E4%B8%AA%E4%BA%8B%E4%BB%B6%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E5%86%B2%E7%AA%81/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130718458" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/determine-if-two-events-have-conflict/solutions/2272942/letmefly-2446pan-duan-liang-ge-shi-jian-glh1m/" target="_blank">LeetCode题解</a>|
865865
|2451.差值数组不同的字符串|简单|<a href="https://leetcode.cn/problems/odd-string-difference/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/25/LeetCode%202451.%E5%B7%AE%E5%80%BC%E6%95%B0%E7%BB%84%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130859334" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/odd-string-difference/solutions/2282785/letmefly-2451chai-zhi-shu-zu-bu-tong-de-ekf6t/" target="_blank">LeetCode题解</a>|
866+
|2452.距离字典两次编辑以内的单词|中等|<a href="https://leetcode.cn/problems/words-within-two-edits-of-dictionary/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/04/22/LeetCode%202452.%E8%B7%9D%E7%A6%BB%E5%AD%97%E5%85%B8%E4%B8%A4%E6%AC%A1%E7%BC%96%E8%BE%91%E4%BB%A5%E5%86%85%E7%9A%84%E5%8D%95%E8%AF%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/160419812" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/words-within-two-edits-of-dictionary/solutions/3957252/letmefly-2452ju-chi-zi-dian-liang-ci-bia-4oev/" target="_blank">LeetCode题解</a>|
866867
|2454.下一个更大元素IV|困难|<a href="https://leetcode.cn/problems/next-greater-element-iv/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/12/LeetCode%202454.%E4%B8%8B%E4%B8%80%E4%B8%AA%E6%9B%B4%E5%A4%A7%E5%85%83%E7%B4%A0IV/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134958992" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/next-greater-element-iv/solutions/2564089/letmefly-2454xia-yi-ge-geng-da-yuan-su-i-m91h/" target="_blank">LeetCode题解</a>|
867868
|2455.可被三整除的偶数的平均值|简单|<a href="https://leetcode.cn/problems/average-value-of-even-numbers-that-are-divisible-by-three/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/29/LeetCode%202455.%E5%8F%AF%E8%A2%AB%E4%B8%89%E6%95%B4%E9%99%A4%E7%9A%84%E5%81%B6%E6%95%B0%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130923191" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/average-value-of-even-numbers-that-are-divisible-by-three/solutions/2288046/letmefly-2455ke-bei-san-zheng-chu-de-ou-ssci7/" target="_blank">LeetCode题解</a>|
868869
|2460.对数组执行操作|简单|<a href="https://leetcode.cn/problems/apply-operations-to-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/05/LeetCode%202460.%E5%AF%B9%E6%95%B0%E7%BB%84%E6%89%A7%E8%A1%8C%E6%93%8D%E4%BD%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131046033" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/apply-operations-to-an-array/solutions/2297527/letmefly-2460dui-shu-zu-zhi-xing-cao-zuo-i8vg/" target="_blank">LeetCode题解</a>|
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: 2452.距离字典两次编辑以内的单词:暴力
3+
date: 2026-04-22 23:11:11
4+
tags: [题解, LeetCode, 中等, 字典树, 数组, 字符串, 暴力, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2452.距离字典两次编辑以内的单词:暴力
9+
10+
力扣题目链接:[https://leetcode.cn/problems/words-within-two-edits-of-dictionary/](https://leetcode.cn/problems/words-within-two-edits-of-dictionary/)
11+
12+
<p>给你两个字符串数组&nbsp;<code>queries</code> 和&nbsp;<code>dictionary</code>&nbsp;。数组中所有单词都只包含小写英文字母,且长度都相同。</p>
13+
14+
<p>一次 <strong>编辑</strong>&nbsp;中,你可以从 <code>queries</code>&nbsp;中选择一个单词,将任意一个字母修改成任何其他字母。从&nbsp;<code>queries</code>&nbsp;中找到所有满足以下条件的字符串:<strong>不超过</strong>&nbsp;两次编辑内,字符串与&nbsp;<code>dictionary</code>&nbsp;中某个字符串相同。</p>
15+
16+
<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>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre><b>输入:</b>queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]
23+
<b>输出:</b>["word","note","wood"]
24+
<strong>解释:</strong>
25+
- 将 "word" 中的 'r' 换成 'o' ,得到 dictionary 中的单词 "wood" 。
26+
- 将 "note" 中的 'n' 换成 'j' 且将 't' 换成 'k' ,得到 "joke" 。
27+
- "ants" 需要超过 2 次编辑才能得到 dictionary 中的单词。
28+
- "wood" 不需要修改(0 次编辑),就得到 dictionary 中相同的单词。
29+
所以我们返回 ["word","note","wood"] 。
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre><b>输入:</b>queries = ["yes"], dictionary = ["not"]
35+
<b>输出:</b>[]
36+
<strong>解释:</strong>
37+
"yes" 需要超过 2 次编辑才能得到 "not" 。
38+
所以我们返回空数组。
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><strong>提示:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= queries.length, dictionary.length &lt;= 100</code></li>
47+
<li><code>n == queries[i].length == dictionary[j].length</code></li>
48+
<li><code>1 &lt;= n &lt;= 100</code></li>
49+
<li>所有&nbsp;<code>queries[i]</code> 和&nbsp;<code>dictionary[j]</code>&nbsp;都只包含小写英文字母。</li>
50+
</ul>
51+
52+
53+
54+
## 解题方法:暴力匹配
55+
56+
两层遍历遍历两个字符串数组,字符串两两尝试匹配。
57+
58+
对于两个字符串,统计相同位置不同字符的个数,若不超过$2$则合格。
59+
60+
合格后记得break,防止重复加入到答案数组中。
61+
62+
+ 时间复杂度$O(n\times len(queries)\times len(dictionary))$
63+
+ 空间复杂度$O(1)$,力扣返回值不计入算法空间复杂度
64+
65+
### AC代码
66+
67+
#### C++
68+
69+
```cpp
70+
/*
71+
* @LastEditTime: 2026-04-22 23:10:53
72+
*/
73+
class Solution {
74+
inline bool ok(const string& a, const string& b) {
75+
int diff = 0;
76+
for (int i = 0; i < a.size(); i++) {
77+
diff += a[i] != b[i];
78+
}
79+
return diff <= 2;
80+
}
81+
public:
82+
vector<string> twoEditWords(vector<string>& queries, vector<string>& dictionary) {
83+
vector<string> ans;
84+
for (string& a : queries) {
85+
for (string& b : dictionary) {
86+
if (ok(a, b)) {
87+
ans.push_back(a);
88+
break;
89+
}
90+
}
91+
}
92+
return ans;
93+
}
94+
};
95+
96+
```
97+
98+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/160419812)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/04/22/LeetCode%202452.%E8%B7%9D%E7%A6%BB%E5%AD%97%E5%85%B8%E4%B8%A4%E6%AC%A1%E7%BC%96%E8%BE%91%E4%BB%A5%E5%86%85%E7%9A%84%E5%8D%95%E8%AF%8D/)哦~
99+
>
100+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)