【LeetCode】1023. Camelcase Matching 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/camelcase-matching/
题目描述
A query word matches a given pattern
if we can insert lowercase letters to the pattern
word so that it equals the query
. (We may insert each character at any position, and may insert 0 characters.)
Given a list of queries
, and a pattern
, return an answer list of booleans, where answer[i]
is true if and only if queries[i]
matches the pattern.
Example 1:
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
Output: [true,false,true,true,false]
Explanation:
"FooBar" can be generated like this "F" + "oo" + "B" + "ar".
"FootBall" can be generated like this "F" + "oot" + "B" + "all".
"FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".
Example 2:
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
Output: [true,false,true,false,false]
Explanation:
"FooBar" can be generated like this "Fo" + "o" + "Ba" + "r".
"FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".
Example 3:
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
Output: [false,true,false,false,false]
Explanation:
"FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".
Note:
1 <= queries.length <= 100
1 <= queries[i].length <= 100
1 <= pattern.length <= 100
- All strings consists only of lower and upper case English letters.
题目大意
判断每个query是不是能通过Pattern的大写字母中插入若干个(可以为0)个小写字母实现。
解题方法
正则+字典
这题的含义其实就是pattern按照大写字母分开,query也按大写字母分开,分开时要保证大写字母是每一段的第一个字符。然后要求pattern中的每一段都是query中每一段的subsequence。
把一个字符串分成一个大写+n个小写的子字符串的方式可以使用正则,表达式是"[A-Z][a-z]*".
判断subsequence需要使用遍历的方式求解,这里学习了lee215的一个写法,iter(qu)这样即可保证顺序查找、找到停止,然后等待下一次查找开始。
Python代码如下:
class Solution(object):
def camelMatch(self, queries, pattern):
"""
:type queries: List[str]
:type pattern: str
:rtype: List[bool]
"""
import re
ps = re.findall("[A-Z][a-z]*", pattern)
N = len(queries)
res = []
for q in queries:
qs = re.findall("[A-Z][a-z]*", q)
hasFind = False
if len(ps) == len(qs):
if all(self.isSubSeq(q, p) for (q, p) in zip(qs, ps)):
hasFind = True
res.append(hasFind)
return res
def isSubSeq(self, qu, pa):
qu = iter(qu)
return all(p in qu for p in pa)
日期
2019 年 4 月 7 日 —— 周赛bug了3次。。
【LeetCode】1023. Camelcase Matching 解题报告(Python)的更多相关文章
- LeetCode: Regular Expression Matching 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
- Leetcode 1023. Camelcase Matching
暴力查找 class Solution: def camelMatch(self, queries: List[str], pattern: str) -> List[bool]: q_size ...
- 【LEETCODE】70、字符匹配1023 Camelcase Matching
最近做leetcode总感觉自己是个智障,基本很少有题能自己独立做出来,都是百度... 不过终于还是做出了一题...而且速度效率还可以 哎,加油吧,尽量锤炼自己 package y2019.Algor ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- 【GS模型】全基因组选择之rrBLUP
目录 1. 理论 2. 实操 2.1 rrBLUP包简介 2.2 实操 3. 补充说明 关于模型 关于交叉验证 参考资料 1. 理论 rrBLUP是基因组选择最常用的模型之一,也是间接法模型的代表.回 ...
- linux 网络配置管理
[1]网络配置基础 (1)用户既可以通过命令行的方式,也可以通过友好的图形界面,轻松完成网络配置. (2)实现Linux网络配置的惟一目标就是修改系统中众多的网络配置文件, 如/etc/interfa ...
- MySQL 的查询优化
说起 MySQL 的查询优化,相信大家收藏了一堆奇技淫巧:不能使用 SELECT *.不使用 NULL 字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解它背 ...
- centos安装后的个人工具
1.安装vim工具 yum -y install vim 安装完成后在家目录下新建一个.vimrc的配置文件.辅助vim软件功能. set number " 显示行号 set cursorl ...
- 03 Windows安装Java环境
Java环境安装 使用微信扫码关注微信公众号,并回复:"Java环境",免费获取下载链接! 1.卸载(电脑未装此程序,跳过此过程) 找到电脑上面的控制面板 找到这两个文 ...
- 表格合并单元格【c#】
gridBranchInfo.DataSource = dtBranchViewList; gridBranchInfo.DataBind(); Random random = new Random( ...
- day07 MySQL索引事务
day07 MySQL索引事务 昨日内容回顾 pymysql模块 # 链接数据库都是使用这个模块的 # 创建链接 import pymysql conn = pymysql.connect( host ...
- 手淘lib-flexible布局适配方案
前置知识:什么是rem CSS3新增的一个相对单位rem(root em,根em).rem是相对于根节点(或者是html节点).如果根节点设置了font-size:10px;那么font-size:1 ...
- JavaScript设计模式,单例模式!
单例设计模式:保证一个类仅有一个实例,并且提供一个访问它的全局访问点.有些对象只需要一个,这时可用单例模式. 传统的单例模式 和new 创建对象的调用不一样 调用者要调用xxx.getInstance ...
- 如何让Linux 机器CPU使用率变高
如何让Linux 机器CPU使用率变高 一.实现 1.单行命令搞定 for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" ...