Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23cea7e

Browse files
✨feat: add 890
1 parent 6b5185b commit 23cea7e

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
4444
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩🤩 |
4545
| [888. 公平的糖果棒交换](https://leetcode-cn.com/problems/fair-candy-swap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/fair-candy-swap/solution/gong-shui-san-xie-yi-ti-shuang-jie-po-su-uant/) | 简单 | 🤩🤩 |
46+
| [890. 查找和替换模式](https://leetcode.cn/problems/find-and-replace-pattern/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-and-replace-pattern/solution/by-ac_oier-s4cw/) | 中等 | 🤩🤩🤩🤩 |
4647
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
4748
| [954. 二倍数对数组](https://leetcode-cn.com/problems/array-of-doubled-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/array-of-doubled-pairs/solution/by-ac_oier-d1z7/) | 中等 | 🤩🤩🤩 |
4849
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
| [868. 二进制间距](https://leetcode-cn.com/problems/binary-gap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-gap/solution/by-ac_oier-2sui/) | 简单 | 🤩🤩🤩🤩 |
104104
| [883. 三维形体投影面积](https://leetcode-cn.com/problems/projection-area-of-3d-shapes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/projection-area-of-3d-shapes/solution/by-ac_oier-r6hj/) | 简单 | 🤩🤩🤩🤩 |
105105
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩 |
106+
| [890. 查找和替换模式](https://leetcode.cn/problems/find-and-replace-pattern/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-and-replace-pattern/solution/by-ac_oier-s4cw/) | 中等 | 🤩🤩🤩🤩 |
106107
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
107108
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩 |
108109
| [929. 独特的电子邮件地址](https://leetcode.cn/problems/unique-email-addresses/) | [LeetCode 题解链接](https://leetcode.cn/problems/unique-email-addresses/solution/by-ac_oier-d3zu/) | 简单 | 🤩🤩🤩 |
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[890. 查找和替换模式](https://leetcode.cn/problems/find-and-replace-pattern/solution/by-ac_oier-s4cw/)** ,难度为 **中等**
4+
5+
Tag : 「哈希表」、「模拟」
6+
7+
8+
9+
你有一个单词列表 `words` 和一个模式 `pattern`,你想知道 `words` 中的哪些单词与模式匹配。
10+
11+
如果存在字母的排列 `p` ,使得将模式中的每个字母 `x` 替换为 `p(x)` 之后,我们就得到了所需的单词,那么单词与模式是匹配的。
12+
13+
(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)
14+
15+
返回 `words` 中与给定模式匹配的单词列表。
16+
17+
你可以按任何顺序返回答案。
18+
19+
示例:
20+
```
21+
输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
22+
23+
输出:["mee","aqq"]
24+
25+
解释:
26+
"mee" 与模式匹配,因为存在排列 {a -> m, b -> e, ...}。
27+
"ccc" 与模式不匹配,因为 {a -> c, b -> c, ...} 不是排列。
28+
因为 a 和 b 映射到同一个字母。
29+
```
30+
31+
提示:
32+
* 1ドル <= words.length <= 50$
33+
* 1ドル <= pattern.length = words[i].length <= 20$
34+
35+
---
36+
37+
### 哈希表 模拟
38+
39+
根据题意进行模拟即可,使用 `map` 记录具体的映射关系,使用 `vis` 记录哪些字符已被映射,利用字符集大小只有 26ドル,ドル我们可以使用数组充当哈希表。
40+
41+
代码:
42+
```Java
43+
class Solution {
44+
public List<String> findAndReplacePattern(String[] ws, String pe) {
45+
List<String> ans = new ArrayList<>();
46+
int[] map = new int[26], vis = new int[26];
47+
for (String s : ws) {
48+
Arrays.fill(map, -1);
49+
Arrays.fill(vis, 0);
50+
boolean ok = true;
51+
for (int i = 0; i < pe.length() && ok; i++) {
52+
int c1 = s.charAt(i) - 'a', c2 = pe.charAt(i) - 'a';
53+
if (map[c1] == -1 && vis[c2] == 0) {
54+
map[c1] = c2; vis[c2] = 1;
55+
} else if (map[c1] != c2) ok = false;
56+
}
57+
if (ok) ans.add(s);
58+
}
59+
return ans;
60+
}
61+
}
62+
```
63+
* 时间复杂度:$O(\sum_{i = 0}^{n}len(ws[i]) + n \times C),ドル其中 $C = 26$ 代表字符集大小
64+
* 空间复杂度:$O(C)$
65+
66+
---
67+
68+
### 最后
69+
70+
这是我们「刷穿 LeetCode」系列文章的第 `No.890` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
71+
72+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
73+
74+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
75+
76+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
77+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /