[LeetCode] 1023. 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.

驼峰式匹配。

如果我们可以将小写字母插入模式串 pattern 得到待查询项 query,那么待查询项与给定模式串匹配。(我们可以在任何位置插入每个字符,也可以插入 0 个字符。)

给定待查询列表 queries,和模式串 pattern,返回由布尔值组成的答案列表 answer。只有在待查项 queries[i] 与模式串 pattern 匹配时, answer[i] 才为 true,否则为 false。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/camelcase-matching
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是双指针,类似找字符串子序列的感觉。对于 input 数组里面的每一个单词 q,我们需要跟 pattern 进行对照。对照的方式是双指针。这里我使用了一个 flag 来记录到底是 true 还是 false。false的条件请参见代码注释。

时间O(mn) - m个单词,平均长度n

空间O(m) - output list

Java实现

 1 class Solution {
 2     public List<Boolean> camelMatch(String[] queries, String pattern) {
 3         List<Boolean> res = new ArrayList<>();
 4         for (String q : queries) {
 5             int i = 0;
 6             int j = 0;
 7             boolean flag = true;
 8             while (i < q.length() && j < pattern.length()) {
 9                 // 当前单词中有大写字母但是pattern里没有
10                 if (q.charAt(i) >= ‘A‘ && q.charAt(i) <= ‘Z‘) {
11                     if (q.charAt(i) != pattern.charAt(j)) {
12                         flag = false;
13                     }
14                 }
15                 if (q.charAt(i) == pattern.charAt(j)) {
16                     j++;
17                 }
18                 i++;
19             }
20             // pattern没走完,false
21             if (j != pattern.length()) {
22                 flag = false;
23             }
24             // 当前单词没走完,有大写字母,false
25             while (i < q.length()) {
26                 if (q.charAt(i) >= ‘A‘ && q.charAt(i) <= ‘Z‘) {
27                     flag = false;
28                     break;
29                 }
30                 i++;
31             }
32             res.add(flag);
33         }
34         return res;
35     }
36 }

 

LeetCode 题目总结

[LeetCode] 1023. Camelcase Matching

上一篇:testNG项目通过idea Terminal命令行执行


下一篇:教你如何使用nginx配置访问wgcloud