You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
A query word matches a given
pattern
if we can insert lowercase letters 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 ananswer
list of booleans, whereanswer[i]
is true if and only ifqueries[i]
matches thepattern
.Example 1:
Example 2:
Example 3:
Note:
1 <= queries.length <= 100
1 <= queries[i].length <= 100
1 <= pattern.length <= 100
这道题说是若要一个 query 单词可以匹配一个模式 pattern 串的话,需要在在 pattern 中加入若干小写字母使其和 query 单词完全相同,现在给了一个单词数组和一个 pattern 串,问数组中的每个 query 单词是否都可以成功匹配。根据题目中的给的例子可以分析出,pattern 串中是可以有小写字母的,但主要是要其中的每个大写字母能匹配上,所以核心是要匹配对应位置的大写字母。由于 query 单词之间没有什么联系,所以每个 query 单词和 pattern 串的匹配方式都是相同的。用一个变量i来指向当前检测到 pattern 串中的位置,用个布尔变量 valid 来标记当前 query 是否能匹配。遍历 query 单词中的字符,若 i 小于 pattern 的长度,且当前字符等于 pattern[i],则i自增1;否则若当前字符是个大写字符,则说明无法匹配,标记 valid 为 false,并 break 掉 for 循环。最后若 valid 为 true,且i正好等于n时,加入 true 到结果 res,否则加入 false,参见代码如下:
解法一:
我们也可以将验证部分放入一个子函数中,整体结构更加清晰一些,但整体思路都是一样的,参见代码如下:
解法二:
Github 同步地址:
#1023
参考资料:
https://leetcode.com/problems/camelcase-matching/
https://leetcode.com/problems/camelcase-matching/discuss/270006/Java-Easy-Two-Pointers
https://leetcode.com/problems/camelcase-matching/discuss/270029/JavaC%2B%2BPython-Check-Subsequence-and-Regax
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: