2019年3月14日 890. Find and Replace Pattern

本文介绍了一种用于字符串模式匹配的算法实现,通过遍历输入的单词列表和模式字符串,使用映射表和已替换集合来判断单词是否符合给定的模式。此算法适用于寻找与特定模式相匹配的所有单词。

简单的字符串判断,水题开启新一天。

class Solution(object):
    def findAndReplacePattern(self, words, pattern):
        """
        :type words: List[str]
        :type pattern: str
        :rtype: List[str]
        """
        ret = []
        replace_map = {}
        replaced = set()
        for word in words:
            replace_map = {}
            replaced = set()
            finded = True
            for i, c in enumerate(pattern):
                if c not in replace_map and word[i] not in replaced:
                    replaced.add(word[i])
                    replace_map[c] = word[i]
                else:
                    if c not in replace_map or word[i] != replace_map[c]:
                        finded = False
                        break
            if finded:
                ret.append(word)
        return ret

转载于:https://www.cnblogs.com/seenthewind/p/10528146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值