1023. 驼峰式匹配【中等】
1. 📝 题目描述
给你一个字符串数组 queries,和一个表示模式的字符串 pattern,请你返回一个布尔数组 answer。只有在待查项 queries[i] 与模式串 pattern 匹配时, answer[i] 才为 true,否则为 false。
如果可以将 小写字母 插入模式串 pattern 得到待查询项 queries[i],那么待查询项与给定模式串匹配。您可以在模式串中的任何位置插入字符,也可以选择不插入任何字符。
示例 1:
txt
输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
输出:[true,false,true,true,false]
示例:
"FooBar" 可以这样生成:"F" + "oo" + "B" + "ar"。
"FootBall" 可以这样生成:"F" + "oot" + "B" + "all".
"FrameBuffer" 可以这样生成:"F" + "rame" + "B" + "uffer".1
2
3
4
5
6
2
3
4
5
6
示例 2:
txt
输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
输出:[true,false,true,false,false]
解释:
"FooBar" 可以这样生成:"Fo" + "o" + "Ba" + "r".
"FootBall" 可以这样生成:"Fo" + "ot" + "Ba" + "ll".1
2
3
4
5
2
3
4
5
示例 3:
txt
输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
输出:[false,true,false,false,false]
解释:
"FooBarTest" 可以这样生成:"Fo" + "o" + "Ba" + "r" + "T" + "est".1
2
3
4
2
3
4
提示:
1 <= pattern.length, queries.length <= 1001 <= queries[i].length <= 100queries[i]和pattern由英文字母组成
2. 🎯 s.1 - 双指针匹配
js
/**
* @param {string[]} queries
* @param {string} pattern
* @return {boolean[]}
*/
var camelMatch = function (queries, pattern) {
const match = (query) => {
let j = 0
for (const c of query) {
if (j < pattern.length && c === pattern[j]) {
j++
} else if (c >= 'A' && c <= 'Z') {
return false
}
}
return j === pattern.length
}
return queries.map(match)
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 时间复杂度:
,其中 是 queries 的长度, 是单个 query 的平均长度 - 空间复杂度:
,不计算输出空间
算法思路:
- 对每个 query,用指针
j遍历 pattern - 遍历 query 中的每个字符:如果与 pattern[j] 匹配则
j++,否则如果是大写字母则匹配失败 - 最终检查
j是否达到 pattern 末尾