用模式的每个字母去当做key对应单词列表的每个字母value,

如果放进dict之前检测到key已经存在,就检测Word[i][j]是否是和已经存在的value一致,不一致就代表不匹配,break检查下一个Word

还有可能不一样的key对应了一样的value,这种情况也要去掉,把dict的value去重一下,比较长度有没有变化,没有变化就代表匹配,最后输出结果。

 class Solution(object):
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
result = []
a = []
flag = True
for i in range(len(words)):
dic = {}
for j in range(len(words[i])):
if pattern[j] not in dic:
dic[pattern[j]] = words[i][j]
elif dic[pattern[j]] != words[i][j]:
flag = False
break
z = set(dic.values()) if flag and len(z) == len(dic):
result.append(words[i])
a.append(dic)
flag = True
return result

leetcode 890. 查找和替换模式 Python的更多相关文章

  1. [LeetCode] 890. Find and Replace Pattern 查找和替换模式

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

  2. [Swift]LeetCode890. 查找和替换模式 | Find and Replace Pattern

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

  3. 【python cookbook】【字符串与文本】6.以不区分大小写的方式对文本做查找和替换

    问题:以不区分大小写的方式对文本做查找和替换 解决方法:使用re模块,并对各种操作都添加上re.IGNORECASE标记 text='UPPER PYTHON,lower python,Mixed P ...

  4. 【python cookbook】【字符串与文本】5.查找和替换文本

    问题:对字符串中的文本做查找和替换 解决方案: 1.对于简单模式:str.replace(old, new[, max]) 2.复杂模式:使用re模块中的re.sub(匹配的模式, newstring ...

  5. [LeetCode] Find And Replace in String 在字符串中查找和替换

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  6. Linux中在vim/vi模式下对文本的查找和替换

    查找: 1.vim  filename  进入一般模式下 2.查找和替换方法 /word    向下查找word 的字符串  例如  /chengtingting   向下查找字符chengtingt ...

  7. [Python] Pandas 对数据进行查找、替换、筛选、排序、重复值和缺失值处理

    目录 1. 数据文件 2. 读数据 3. 查找数据 4. 替换数据 4.1 一对一替换 4.2 多对一替换 4.3 多对多替换 5. 插入数据 6. 删除数据 6.1 删除列 6.2 删除行 7. 处 ...

  8. 【python】Leetcode每日一题-132模式

    [python]Leetcode每日一题-132模式 [题目描述] 给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j &l ...

  9. Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)

    833. 字符串中的查找与替换 对于某些字符串 S,我们将执行一些替换操作,用新的字母组替换原有的字母组(不一定大小相同). 每个替换操作具有 3 个参数:起始索引 i,源字 x 和目标字 y.规则是 ...

随机推荐

  1. Linux命令 ls 和 ll 的使用方法与基本区别

    Linux 命令 ls 和 ll 的使用方法: ll:罗列出当前文件或目录的详细信息,含有时间.读写权限.大小.时间等信息 ,像Windows显示的详细信息.ll是“ls -l"的别名.相当 ...

  2. Centos解除端口占用

    - 查看所有端口占用 - netstat -tln - 查看端口被哪个进程占用 - lsof -i:端口号 - 杀死被占用端口 - kill 端口号

  3. Notepad++的实用技巧

    一. 安装notepad + +  notepad++的下载.安装非常easy.下一步下一步,所有选项都默认就可以安装好.但有几点需要注意.   截止到写这篇博文,notepad++的最新版本为7.5 ...

  4. iOS证书申请及使用详细说明

    iOS 证书申请和使用详解(详细版)阅读   对于iOS开发者来说,apple开发者账号肯定不会陌生.在开发中我们离不开它.下面我简单的为大家分享一下关于iOS开发中所用的证书相关知识. 第一部分:成 ...

  5. Spring - AOP简介与图示

    [1]AOP (Aspect-Oriented Programming, 面向切面编程),是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) ...

  6. ansible 下lineinfile详细使用

    ansible 下lineinfile详细使用 时间 2016-12-13 18:02:31  51CTO推荐博文 原文  http://zouqingyun.blog.51cto.com/78224 ...

  7. 开始Python学习

    主要结合ArcGIS进行空间数据处理 Python最讨厌的就是版本问题了 ArcGIS 10.5安装的时候已经安装了python2.7.13,但后来又安装了python3.6.1. 环境变量的设置: ...

  8. KeyguardSliceView.java

    /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  9. 最详细的 linux grep命令教程

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  10. 复习下CSS-零碎要点

    一,CSS选择器 1.  h1 > strong {color:red;}表示的是只有h1下子元素才是红色,“孙子”就不行. 2.  h1 + p {margin-top:50px;}   选择 ...