LC.P1023[驼峰式匹配]

方法一:双指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<Boolean> camelMatch(String[] queries, String pattern) {
List<Boolean> ans = new ArrayList<>();
int length = pattern.length();
for (String query : queries) {
int n = query.length(), j = 0;
boolean flag = true;
for (int i = 0; i < n; ++i) {
char c = query.charAt(i);
if (j < length && pattern.charAt(j) == c) ++j;
else if (Character.isUpperCase(c)) {
flag = false;
break;
}
}
if (j != length) flag = false;
ans.add(flag);
}
return ans;
}
}
  • 时间复杂度:$O(mn)$,其中$n$为$queries$的长度,$m$为$queries[i]$的长度
  • 空间复杂度:$O(1)$