Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d3a8102

Browse files
committedMay 24, 2025·
一刷890
1 parent c782037 commit d3a8102

File tree

5 files changed

+96
-33
lines changed

5 files changed

+96
-33
lines changed
 

‎README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6255,14 +6255,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
62556255
//|{doc_base_url}/0889-construct-binary-tree-from-preorder-and-postorder-traversal.adoc[题解]
62566256
//|Medium
62576257
//|
6258-
//
6259-
//|{counter:codes}
6260-
//|{leetcode_base_url}/find-and-replace-pattern/[890. Find and Replace Pattern^]
6261-
//|{source_base_url}/_0890_FindAndReplacePattern.java[Java]
6262-
//|{doc_base_url}/0890-find-and-replace-pattern.adoc[题解]
6263-
//|Medium
6264-
//|
6265-
//
6258+
6259+
|{counter:codes}
6260+
|{leetcode_base_url}/find-and-replace-pattern/[890. Find and Replace Pattern^]
6261+
|{source_base_url}/_0890_FindAndReplacePattern.java[Java]
6262+
|{doc_base_url}/0890-find-and-replace-pattern.adoc[题解]
6263+
|Medium
6264+
|
6265+
62666266
//|{counter:codes}
62676267
//|{leetcode_base_url}/sum-of-subsequence-widths/[891. Sum of Subsequence Widths^]
62686268
//|{source_base_url}/_0891_SumOfSubsequenceWidths.java[Java]
Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
11
[#0890-find-and-replace-pattern]
2-
= 890. Find and Replace Pattern
2+
= 890. 查找和替换模式
33

4-
{leetcode}/problems/find-and-replace-pattern/[LeetCode - Find and Replace Pattern^]
4+
https://leetcode.cn/problems/find-and-replace-pattern/[LeetCode - 890. 查找和替换模式 ^]
55

6-
You have a list of `words` and a `pattern`, and you want to know which words in `words` matches the pattern.
6+
你有一个单词列表 `words` 和一个模式 `pattern`,你想知道 `words` 中的哪些单词与模式匹配。
77

8-
A word matches the pattern if there exists a permutation of letters `p` so that after replacing every letter `x` in the pattern with `p(x)`, we get the desired word.
8+
如果存在字母的排列 `p` ,使得将模式中的每个字母 `x` 替换为 `p(x)` 之后,我们就得到了所需的单词,那么单词与模式是匹配的。
99

10-
(_Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter._)
10+
_(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)_
1111

12-
Return a list of the words in `words` that match the given pattern.
12+
返回 `words` 中与给定模式匹配的单词列表。
1313

14-
You may return the answer in any order.
14+
你可以按任何顺序返回答案。
1515

16-
16+
*示例:*
1717

18+
....
19+
输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
20+
输出:["mee","aqq"]
21+
解释:
22+
"mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。
23+
"ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。
24+
因为 a 和 b 映射到同一个字母。
25+
....
1826

19-
*Example 1:*
2027

21-
[subs="verbatim,quotes,macros"]
22-
----
23-
*Input:* words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
24-
*Output:* ["mee","aqq"]
25-
*Explanation:* "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
26-
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
27-
since a and b map to the same letter.
28-
----
29-
30-
31-
32-
*Note:*
28+
*提示:*
3329

34-
35-
* `1 <= words.length <= 50`
36-
* `1 <= pattern.length = words[i].length <= 20`
30+
* `+1 <= words.length <= 50+`
31+
* `+1 <= pattern.length = words[i].length <= 20+`
3732
3833
34+
== 思路分析
3935

36+
建立一个字母映射,并且每个字母只能有一种映射关系。否则就不匹配。
4037

38+
看官方题解,可以建立双向映射: `src → tgt` 和 `tgt → src`,如果都可以,则匹配。这样的方式更简单。
4139

4240
[[src-0890]]
41+
[tabs]
42+
====
43+
一刷::
44+
+
45+
--
4346
[{java_src_attr}]
4447
----
4548
include::{sourcedir}/_0890_FindAndReplacePattern.java[tag=answer]
4649
----
50+
--
51+
52+
// 二刷::
53+
// +
54+
// --
55+
// [{java_src_attr}]
56+
// ----
57+
// include::{sourcedir}/_0890_FindAndReplacePattern_2.java[tag=answer]
58+
// ----
59+
// --
60+
====
61+
62+
63+
== 参考资料
4764

65+
. https://leetcode.cn/problems/find-and-replace-pattern/solutions/1593882/cha-zhao-he-ti-huan-mo-shi-by-leetcode-s-fyyg/[890. 查找和替换模式 - 查找和替换模式^]
66+
. https://leetcode.cn/problems/find-and-replace-pattern/solutions/1595777/by-ac_oier-s4cw/[890. 查找和替换模式 - 简单哈希表模拟题^]

‎docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ include::0881-boats-to-save-people.adoc[leveloffset=+1]
18631863

18641864
// include::0889-construct-binary-tree-from-preorder-and-postorder-traversal.adoc[leveloffset=+1]
18651865

1866-
// include::0890-find-and-replace-pattern.adoc[leveloffset=+1]
1866+
include::0890-find-and-replace-pattern.adoc[leveloffset=+1]
18671867

18681868
// include::0891-sum-of-subsequence-widths.adoc[leveloffset=+1]
18691869

‎logbook/202503.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,12 @@ endif::[]
808808
|{doc_base_url}/0841-keys-and-rooms.adoc[题解]
809809
|✅ 深度优先遍历或广度优先遍历。
810810

811+
|{counter:codes2503}
812+
|{leetcode_base_url}/find-and-replace-pattern/[890. 查找和替换模式^]
813+
|{doc_base_url}/0890-find-and-replace-pattern.adoc[题解]
814+
|✅ 映射。建立一个字母映射,并且每个字母只能有一种映射关系。否则就不匹配。
815+
816+
811817

812818
|===
813819

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.*;
4+
5+
public class _0890_FindAndReplacePattern {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-05-24 20:56:15
11+
*/
12+
public List<String> findAndReplacePattern(String[] words, String pattern) {
13+
List<String> result = new ArrayList<>();
14+
for (String word : words) {
15+
StringBuilder sb = new StringBuilder(word);
16+
Set<Character> set = new HashSet<>();
17+
Map<Character, String> map = new HashMap<>();
18+
for (int i = 0; i < word.length(); i++) {
19+
char pc = pattern.charAt(i);
20+
char wc = word.charAt(i);
21+
if (!map.containsKey(wc) && !set.contains(pc)) {
22+
map.put(wc, String.valueOf(pc));
23+
set.add(pc);
24+
sb.replace(i, i + 1, map.get(wc));
25+
} else if (map.containsKey(wc)) {
26+
sb.replace(i, i + 1, map.get(wc));
27+
} else {
28+
break;
29+
}
30+
}
31+
if (sb.toString().equals(pattern)) {
32+
result.add(word);
33+
}
34+
}
35+
return result;
36+
}
37+
// end::answer[]
38+
}

0 commit comments

Comments
 (0)
Please sign in to comment.