LeetCode 890. 查找和替换模式(模拟+双哈希表)


题目

在这里插入图片描述

解法:模拟+双哈希表

**整体思路:**遍历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 的长度,mpattern 的长度。对于每个 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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xylitolz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值