首页 > 技术文章 > 2019年3月14日 890. Find and Replace Pattern

seenthewind 2019-03-14 09:27 原文

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

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

推荐阅读