题目
标题和出处
标题:查找和替换模式
出处:890. 查找和替换模式
难度
3 级
题目描述
要求
给你一个单词列表 words \texttt{words} words 和一个字符串 pattern \texttt{pattern} pattern,返回与 pattern \texttt{pattern} pattern 匹配的列表 words[i] \texttt{words[i]} words[i]。你可以按任何顺序返回答案。
如果存在字母的排列 p \texttt{p} p 使得将模式中的每个字母 x \texttt{x} x 替换为 p(x) \texttt{p(x)} p(x) 之后能得到所需的单词,那么单词与模式是匹配的。
注意字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。
示例
示例 1:
输入:
words
=
["abc","deq","mee","aqq","dkd","ccc"],
pattern
=
"abb"
\texttt{words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"}
words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
输出:
["mee","aqq"]
\texttt{["mee","aqq"]}
["mee","aqq"]
解释:
"mee"
\texttt{"mee"}
"mee" 与模式匹配,因为存在排列
{a
→
m,
b
→
e,
…
}
\texttt{\{a} \rightarrow \texttt{m, b} \rightarrow \texttt{e, \ldots\}}
{a→m, b→e, …}。
"ccc"
\texttt{"ccc"}
"ccc" 与模式不匹配,因为
{a
→
c,
b
→
c,
…
}
\texttt{\{a} \rightarrow \texttt{c, b} \rightarrow \texttt{c, \ldots\}}
{a→c, b→c, …} 不是排列,由于
a
\texttt{a}
a 和
b
\texttt{b}
b 映射到同一个字母。
示例 2:
输入:
words
=
["a","b","c"],
pattern
=
"a"
\texttt{words = ["a","b","c"], pattern = "a"}
words = ["a","b","c"], pattern = "a"
输出:
["a","b","c"]
\texttt{["a","b","c"]}
["a","b","c"]
数据范围
- 1 ≤ pattern.length ≤ 20 \texttt{1} \le \texttt{pattern.length} \le \texttt{20} 1≤pattern.length≤20
- 1 ≤ words.length ≤ 50 \texttt{1} \le \texttt{words.length} \le \texttt{50} 1≤words.length≤50
- words[i].length = pattern.length \texttt{words[i].length} = \texttt{pattern.length} words[i].length=pattern.length
- pattern \texttt{pattern} pattern 和 words[i] \texttt{words[i]} words[i] 由小写英语字母组成
解法
思路和算法
这道题中的模式匹配规则和「同构字符串」中的同构规则相同,两个字符串匹配等价于两个字符串满足双射关系。
遍历数组 words \textit{words} words,判断其中的每个单词和模式 pattern \textit{pattern} pattern 是否匹配,遇到匹配的单词则加入返回列表。
代码
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> matchWords = new ArrayList<String>();
for (String word : words) {
if (match(pattern, word)) {
matchWords.add(word);
}
}
return matchWords;
}
public boolean match(String pattern, String word) {
Map<Character, Character> map1 = new HashMap<Character, Character>();
Map<Character, Character> map2 = new HashMap<Character, Character>();
int length = pattern.length();
for (int i = 0; i < length; i++) {
char c1 = pattern.charAt(i);
char c2 = word.charAt(i);
if (!map1.containsKey(c1)) {
map1.put(c1, c2);
} else if (map1.get(c1) != c2) {
return false;
}
if (!map2.containsKey(c2)) {
map2.put(c2, c1);
} else if (map2.get(c2) != c1) {
return false;
}
}
return true;
}
}
复杂度分析
-
时间复杂度: O ( m n ) O(mn) O(mn),其中 m m m 是数组 words \textit{words} words 中的每个单词的长度和 pattern \textit{pattern} pattern 的长度, n n n 是数组 words \textit{words} words 的长度。对于数组 words \textit{words} words 中的每个单词需要 O ( m ) O(m) O(m) 的时间判断是否和模式匹配,共有 n n n 个单词,因此时间复杂度是 O ( m n ) O(mn) O(mn)。
-
空间复杂度: O ( m ) O(m) O(m),其中 m m m 是数组 words \textit{words} words 中的每个单词的长度和 pattern \textit{pattern} pattern 的长度。空间复杂度主要取决于哈希表存储的对应关系,由于每个单词和 pattern \textit{pattern} pattern 的长度都是 m m m,因此需要 O ( m ) O(m) O(m) 的空间,注意返回值不计入空间复杂度。