class Solution:
def camelMatch(self, queries, pattern):
def is_match(query, pattern):
i = 0
for c in query:
if i < len(pattern) and c == pattern[i]:
i += 1
elif c.isupper():
return False
return i == len(pattern)
return [is_match(query, pattern) for query in queries]
class Solution {
public:
bool isMatch(const string& query, const string& pattern) {
int i = 0;
for (char c : query) {
if (i < pattern.size() && c == pattern[i]) {
++i;
} else if (isupper(c)) {
return false;
}
}
return i == pattern.size();
}
vector<bool> camelMatch(vector<string>& queries, string pattern) {
vector<bool> res;
for (const string& query : queries) {
res.push_back(isMatch(query, pattern));
}
return res;
}
};
class Solution {
public List<Boolean> camelMatch(String[] queries, String pattern) {
List<Boolean> res = new ArrayList<>();
for (String query : queries) {
res.add(isMatch(query, pattern));
}
return res;
}
private boolean isMatch(String query, String pattern) {
int i = 0;
for (char c : query.toCharArray()) {
if (i < pattern.length() && c == pattern.charAt(i)) {
i++;
} else if (Character.isUpperCase(c)) {
return false;
}
}
return i == pattern.length();
}
}
var camelMatch = function(queries, pattern) {
function isMatch(query, pattern) {
let i = 0;
for (let c of query) {
if (i < pattern.length && c === pattern[i]) {
i++;
} else if (c >= 'A' && c <= 'Z') {
return false;
}
}
return i === pattern.length;
}
return queries.map(query => isMatch(query, pattern));
};
queries
and a string pattern
, determine for each query whether it matches the pattern using camelcase rules. A query matches the pattern if we can insert any number of lowercase English letters at any position in the pattern to obtain the query, but we cannot insert uppercase letters or change the order of characters. Every uppercase letter in the pattern must appear in the query at the same position, and any extra uppercase letter in the query that does not appear in the pattern causes the match to fail.
The function should return a list of booleans, one for each query in queries
, indicating whether the query matches the pattern.
queries
, check if it matches the pattern
using a helper function.
Suppose queries = ["FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"]
and pattern = "FB"
.
The result would be [True, False, True, True, False]
.
Q
queries and average length L
, the time complexity is O(Q * L).