leetcode 890. 查找和替换模式 Python
用模式的每个字母去当做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的更多相关文章
- [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 ...
- [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 ...
- 【python cookbook】【字符串与文本】6.以不区分大小写的方式对文本做查找和替换
问题:以不区分大小写的方式对文本做查找和替换 解决方法:使用re模块,并对各种操作都添加上re.IGNORECASE标记 text='UPPER PYTHON,lower python,Mixed P ...
- 【python cookbook】【字符串与文本】5.查找和替换文本
问题:对字符串中的文本做查找和替换 解决方案: 1.对于简单模式:str.replace(old, new[, max]) 2.复杂模式:使用re模块中的re.sub(匹配的模式, newstring ...
- [LeetCode] Find And Replace in String 在字符串中查找和替换
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- Linux中在vim/vi模式下对文本的查找和替换
查找: 1.vim filename 进入一般模式下 2.查找和替换方法 /word 向下查找word 的字符串 例如 /chengtingting 向下查找字符chengtingt ...
- [Python] Pandas 对数据进行查找、替换、筛选、排序、重复值和缺失值处理
目录 1. 数据文件 2. 读数据 3. 查找数据 4. 替换数据 4.1 一对一替换 4.2 多对一替换 4.3 多对多替换 5. 插入数据 6. 删除数据 6.1 删除列 6.2 删除行 7. 处 ...
- 【python】Leetcode每日一题-132模式
[python]Leetcode每日一题-132模式 [题目描述] 给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j &l ...
- Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
833. 字符串中的查找与替换 对于某些字符串 S,我们将执行一些替换操作,用新的字母组替换原有的字母组(不一定大小相同). 每个替换操作具有 3 个参数:起始索引 i,源字 x 和目标字 y.规则是 ...
随机推荐
- 关于Hibernate级联更新插入信息时提示主键不为空的问题“org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1 ”
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual ...
- 服务器配置+wordpress建站(小白)
一. 安装好centos7.2系统后,登录centos系统输入如下命令: yum install -y wget && wget -O install.sh http://downlo ...
- css缩放的坑
transform:scale()缩放后会往中间聚集.transform-origin: top left可以让他看上去是沿着左上角缩放. .zoom { transform: scale(.8); ...
- java web从入门到精通
1.Springboot配置 1.1mybatis mapper.xml所在的目录必须为resource的资源文件夹,如果xml文件在java的package里面,需要修改文件夹类型 idea修改方式 ...
- 2PC(Two Phase Commitment Protocol)原理
读TiDB原理部分,知道其分布式事务是参考的Google percolator.而percolator是一种2PC的优化. 分布式事务解决的是什么问题呢? 假设一个场景,一个电商网站,用户在购买商品时 ...
- 每个月总有那么几天!!!!XML解析
1. 介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许 ...
- GitHub Desktop下载及使用
GitHub Desktop下载及使用 用了几次 GitHub Desktop 之后,发现不好用,其图形化界面功能有限.推荐使用Git for Windows,官方网站 https://git-f ...
- amoeba 使用笔记
环境 延用MySQL 主主+主从笔记的环境 Java version “1.8.0_73” 安装 wget http://nchc.dl.sourceforge.net/project/amoeba/ ...
- FlappyBird开发帮助文档
FlappyBird开发帮助文档 项目需求 完成FlappyBird游戏. 功能说明: 游戏开始后,间歇性的点击鼠标,让小鸟向上飞,不会掉下来,并且要穿过柱子的空隙,不能碰到柱子,碰到就dead了,穿 ...
- 2018-2019-2 20165313 《网络对抗技术》Exp4 恶意代码分析
一.实践目标 1.监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systrac ...