【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 ...
随机推荐
- 安装依赖的时候,报错npm WARN checkPermissions
解决办法1 . 删除node_modules文件夹,重新安装依赖. 解决办法2 . 统一使用同一个npm安装依赖 . 原因:有的依赖包是用npm安装的,有的依赖包是用cnpm安装的.
- leetcode 125. 验证回文串(python)
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...
- storm集群搭建和java应用
1. vim /etc/hosts ssh免密登录192.168.132.154 c0192.168.132.156 c1192.168.132.155 c2 storm集群:192.168.132. ...
- 判断文件是否存在的shell脚本代码!
实现代码一 #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/Top" ]; then mkdir -p /Topfi #shell判断文件, ...
- 关于Vue+iview的简单下拉框滚动加载
话不多说,直接上代码,作用是下拉框内容无限滚动加载: Html: <FormItem style="position:relative" label="用户名:&q ...
- Altium Designer chapter5总结
PCB设计环境中需要注意的如下: (1)PCB设计步骤:绘制原理图和生成网表—规划电路板—载入网表—元件布局—制定设计规则—布线—后期处理—DRC检查—信号完整性分析—gerbera文件输出 (2)P ...
- jdk (Java Development Kit)
JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具. JD ...
- SEC1- 数据库的相关概念
一.数据库的好处1. 可以持久化数据到本地2. 结构化查询 二.数据库的常见概念1. DB:data base数据库,存储数据的容器2. DBMS:database management sysy ...
- 【python】随机数相关
http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html 该博文写的很详细,备忘. 需要注意的是,写测试脚本的时候,不要将脚本命名成跟 ...
- C#调取接口时报错:服务器提交了协议冲突. Section=ResponseStatusLine
private Dictionary<string, Object> GetLocation(string imei) { #region===代码=== string serviceAd ...