当前位置:网站首页>LeetCode 890. 查找和替换模式(模拟+双哈希表)
LeetCode 890. 查找和替换模式(模拟+双哈希表)
2022-06-12 11:46:00 【xylitolz】
题目

解法:模拟+双哈希表
**整体思路:**遍历words,逐个判断每个单词是否与pattern匹配
在逐个判断过程中,使用双map构造从字母到字母的映射,即 word 的每个字母需要映射到 pattern 的对应字母,并且 pattern 的每个字母也需要映射到 word 的对应字母。
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> res = new ArrayList<>();
int n = words.length;
char[] pcs = pattern.toCharArray();
for (int i = 0; i < n; i++) {
char[] cs = words[i].toCharArray();
if (cs.length != pcs.length) {
continue ;
}
// pattern -> word
Map<Character, Character> p2wMap = new HashMap<>();
// word -> pattern
Map<Character, Character> w2pMap = new HashMap<>();
boolean flag = true;
for (int j = 0; j < cs.length; j++) {
char a = pcs[j], b = cs[j];
if (p2wMap.containsKey(a)) {
if (p2wMap.get(a) != b ) {
flag = false;
break;
}
} else {
p2wMap.put(a, b);
}
if (w2pMap.containsKey(b)) {
if (w2pMap.get(b) != a) {
flag = false;
break;
}
} else {
w2pMap.put(b, a);
}
}
if (flag) {
res.add(words[i]);
}
}
return res;
}
}
时间复杂度:
O(nm),其中n是数组words的长度,m是pattern的长度。对于每个word需要O(m)的时间检查其是否与pattern匹配。空间复杂度:
O(m)。哈希表需要O(m)的空间
也可以利用字符集大小只有 26,进而使用数组充当哈希表,使用 map 记录具体的映射关系,使用 vis 记录哪些字符已被映射
class Solution {
public List<String> findAndReplacePattern(String[] ws, String pe) {
List<String> ans = new ArrayList<>();
int[] map = new int[26], vis = new int[26];
for (String s : ws) {
Arrays.fill(map, -1);
Arrays.fill(vis, 0);
boolean ok = true;
for (int i = 0; i < pe.length() && ok; i++) {
int c1 = s.charAt(i) - 'a', c2 = pe.charAt(i) - 'a';
if (map[c1] == -1 && vis[c2] == 0) {
map[c1] = c2; vis[c2] = 1;
} else if (map[c1] != c2) {
ok = false;
}
}
if (ok) {
ans.add(s);
}
}
return ans;
}
}
Reference
边栏推荐
- C# 37. Textbox scroll bar and multiline
- Network topology
- Process creation and recycling
- Ficusjs series (I) introduction to ficusjs
- Simple solution of regular expression
- K58. Chapter 1 installing kubernetes V1.23 based on kubeadm -- cluster deployment
- 6.6 分離卷積
- K52. Chapter 1: installing kubernetes v1.22 based on kubeadm -- cluster deployment
- ARM指令集之数据处理指令寻址方式
- When you have a server
猜你喜欢

PIP install in the CONDA environment cannot be installed into the specified CONDA environment (the default PIP installation location of the CONDA environment)

正则表达式 | 浅解

When you have a server

C# 35. Select default network card

B.刷墙(C语言)

5G NR协议学习--TS38.211下行通道

QML学习 第一天

UML series articles (31) architecture modeling - deployment diagram

你需要社交媒体二维码的21个理由

Analyze the implementation principle of the onion model, and connect the onion model in your own project
随机推荐
ARM指令集之杂类指令
C# 37. textbox滚动条与多行
Socket Programming TCP
淘宝新改版商家如何操作,需要注意的点有哪些
机器学习之决策树
UML系列文章(31)体系结构建模---部署图
ARM指令集之伪指令
6.6 Convolution de séparation
Basic principle of Doppler effect
Index in MySQL show index from XXX the meaning of each parameter
UML series articles (30) architecture modeling -- product diagram
Deep learning and CV tutorial (14) | image segmentation (FCN, segnet, u-net, pspnet, deeplab, refinenet)
TinyMCE realizes automatic uploading of pasted pictures
Arm cross compilation chain download address
Differences among various cross compiling tools of arm
Naming specification / annotation specification / logical specification
字节序(网络/主机)转换
机器学习基础概念
Lambda expression | shallow solution
First understand the onion model, analyze the implementation process of middleware, and analyze the source code of KOA Middleware