作者: 负雪明烛
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. 1 <= queries.length <= 100
  2. 1 <= queries[i].length <= 100
  3. 1 <= pattern.length <= 100
  4. 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)的更多相关文章

  1. LeetCode: Regular Expression Matching 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

  2. Leetcode 1023. Camelcase Matching

    暴力查找 class Solution: def camelMatch(self, queries: List[str], pattern: str) -> List[bool]: q_size ...

  3. 【LEETCODE】70、字符匹配1023 Camelcase Matching

    最近做leetcode总感觉自己是个智障,基本很少有题能自己独立做出来,都是百度... 不过终于还是做出了一题...而且速度效率还可以 哎,加油吧,尽量锤炼自己 package y2019.Algor ...

  4. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. 2基因组间鉴定SV

    本文学习费章军老师文章Genome of Solanum pimpinellifolium provides insights into structural variants during toma ...

  2. hash_map,map,unordered_map效率

    利用unordered_map代替hash_map 实验环境 操作系统 fedora9 编译器版本 gcc4.3 实验方式 各种map使用插入和查找,比较速度和相关性能 代码 参考代码 下面测试说明了 ...

  3. JuiceFS 数据读写流程详解

    对于文件系统而言,其读写的效率对整体的系统性能有决定性的影响,本文我们将通过介绍 JuiceFS 的读写请求处理流程,让大家对 JuiceFS 的特性有更进一步的了解. 写入流程 JuiceFS 对大 ...

  4. 第二个基础框架 — spring — xml版,没用注解 — 更新完毕

    1.什么是spring? 老规矩:百度百科一手 这上面说得太多了,我来提炼一下: spring就是一个轻量级的控制反转( IOC ) 和 面向切面编程( AOP ) 的容量框架.总的来说:本质就是对j ...

  5. LVM磁盘创建与扩容

    以虚拟机为例 1.在虚拟机上添加新磁盘,点击虚拟机→设置->添加,最后如下图. 2.进入系统fdisk -l,查看当前磁盘信息 [root@master shell]# fdisk -l Dis ...

  6. 零基础学习java------day18------properties集合,多线程(线程和进程,多线程的实现,线程中的方法,线程的声明周期,线程安全问题,wait/notify.notifyAll,死锁,线程池),

    1.Properties集合 1.1 概述: Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串 一个属性列表可包含另 ...

  7. 机器学习常用python包

    (py37) ai@ai:~$ pip freeze |grep -v '@' astor==0.8.1 certifi==2021.5.30 chardet==4.0.0 cycler==0.10. ...

  8. [项目总结]论Android Adapter notifyDataSetChanged与notifyDataSetInvalidated无效原因

    最近在开发中遇到一个问题,Adapter中使用notifyDataSetChanged 与notifyDataSetInvalidated无效,经过思考和网上查找,得出如下原因. 首先看一下notif ...

  9. oracle 外部表查alter日志

    --创建文件夹,路径是alter日志的路径 create or replace directory data_dir as '/u01/app/oracle/diag/rdbms/orcl/orcl/ ...

  10. Advanced C++ | Virtual Copy Constructor

    这个不懂,等看会了再写...