Problem
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:
1 | Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB" |
Example 2:
1 | Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa" |
Example 3:
1 | Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT" |
Note:
1 <= queries.length <= 1001 <= queries[i].length <= 1001 <= pattern.length <= 100- All strings consists only of lower and upper case English letters.
Analysis
驼峰命名法的字符串匹配,题目给出一个pattern以及一系列的queries,如果query是在pattern的字母之间添加小写字母,则合法;如果添加的是大写字母,则不合法。
我们对每个查询词遍历,首先判断是否和pattern中的字母匹配。如果匹配则到下一个字母;否则检查大小写,小写的可以忽略,遇到大写则说明匹配错误,可以立即中止匹配过程,返回false。
Solution
由于需要记录查询词和pattern匹配到哪一个字母,所以每个查询词都需要有一个下标k来记录。同时需要注意,遍历完后有可能是查询词并没有出现pattern中所有的字母,所以还要比对k和pattern的长度。
Code
1 | class Solution { |
Summary
这道题字符串匹配类型的题目,题目的描述比较少见,但其实本质还是不变的,仍然是通过遍历与比对解决。在面对字符串匹配问题的时候,我们要善于抓住匹配的点,即什么时候匹配失败。抓好这个点就能够写出判断的条件,题目也就迎刃而解。这道题目就和大家分享到这里,欢迎大家评论、转发,谢谢您的支持!