【leetcode】1023. Camelcase Matching
题目如下:
A query word matches a given
patternif we can insert lowercaseletters to the pattern word so that it equals thequery. (We may insert each character at any position, and may insert 0 characters.)Given a list of
queries, and apattern, return ananswerlist of booleans, whereanswer[i]is true if and only ifqueries[i]matches thepattern.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 <= 1001 <= queries[i].length <= 1001 <= pattern.length <= 100- All strings consists only of lower and upper case English letters.
解题思路:这种模式匹配的题目,感觉还是用正则表达式简单些。例如 pattern = "FoBaT",转换成正则表达式的pattern="^[a-z]*F[a-z]*o[a-z]*B[a-z]*a[a-z]*T[a-z]*$"。
代码如下:
class Solution(object):
def camelMatch(self, queries, pattern):
"""
:type queries: List[str]
:type pattern: str
:rtype: List[bool]
"""
res = []
import re
rePattern = '^[a-z]*'
for i,v in enumerate(pattern):
rePattern += v
rePattern += '[a-z]*'
#rePattern = rePattern[:-2]
rePattern += '$'
for i in queries:
r = re.search(rePattern, i)
if r == None:
res.append(False)
continue
r = r.group()
subq = i[len(r):]
res.append(len(subq) == 0 or subq.islower())
return res
【leetcode】1023. Camelcase Matching的更多相关文章
- 【LeetCode】1023. Camelcase Matching 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+字典 日期 题目地址:https://leet ...
- 【leetcode】44. Wildcard Matching
题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】1023. Binary String With Substrings Representing 1 To N
题目如下: Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, r ...
- 【LEETCODE】70、字符匹配1023 Camelcase Matching
最近做leetcode总感觉自己是个智障,基本很少有题能自己独立做出来,都是百度... 不过终于还是做出了一题...而且速度效率还可以 哎,加油吧,尽量锤炼自己 package y2019.Algor ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- 31 October
https://www.cnblogs.com/RabbitHu/p/51nod1353.html 树形 DP 求所有联通块 \(\ge K\) 的方案数. 切断:\(\forall i\in\lef ...
- 笨办法学Python(learn python the hard way)--练习程序21-30
下面是练习21-30,基于python3 #ex21.py 1 def add(a, b): print("ADDING %d + %d" %(a, b)) return a+b ...
- jQuery入门教程-文档操作方法
一.append()和appendTo() 1.1 append()方法 <body> <p>好好学习</p> <button>append() 方法& ...
- jenkins之启动与关闭
jenkins可以通过内置的应用服务器或者借助其他应用服务器启动 目录 1.启动jenkins 2.关闭jenkins 3.重启jenkins 4.重新加载jenkins配置信息 1.启动jenkin ...
- Delphi保存网页中的图片
WEBBrowser已经打开了URL V = WEBBrowser.Document.body.createControlRange(); V1 = WEBBrow ...
- CentOS 7安装图形界面
之前公司的服务器都是用的CentOS 的系统,需要安装图形界面的时候我会执行以下命令 yum -y groupinstall "X Window System" "Fon ...
- SEC4 - MySQL语法规范
1.不区分大小写,但是建议关键字大写,表名.列名小写. 2.每条命令最好分号结尾 3.每条命令根据需要,可以进行缩进或者换行 4.注释 单行注释:#注释文字 单行注释:--注释文字 多行注释: /* ...
- Ubuntu下使用boost例子
http://blog.csdn.net/dotphoenix/article/details/8459277 1. 安装boost库 sudo apt-get install libboost-al ...
- 单例模式(Singleton Patten)
顾名思义,单例模式就是只有一个实例,不管怎样,使用了单例模式的类在系统中只有一个对象被访问到.Java中单例模式定义:“一个类有且仅有一个实例,并且这个类会自行实例化,实例化时候的对象可以提供给整个系 ...
- vuejs基础-跑马灯效果
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...