哈希表题目:查找和替换模式

题目

标题和出处

标题:查找和替换模式

出处: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\}} {am, be, }
"ccc" \texttt{"ccc"} "ccc" 与模式不匹配,因为 {a → c,   b → c,   … } \texttt{\{a} \rightarrow \texttt{c, b} \rightarrow \texttt{c, \ldots\}} {ac, bc, } 不是排列,由于 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} 1pattern.length20
  • 1 ≤ words.length ≤ 50 \texttt{1} \le \texttt{words.length} \le \texttt{50} 1words.length50
  • 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) 的空间,注意返回值不计入空间复杂度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟大的车尔尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值