LeetCode Problem
1023. Camelcase Matching
Link to LeetCode
Given an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise.
A query word queries[i] matches pattern if you can insert lowercase English letters into the pattern so that it equals the query. You may insert a character at any position in pattern or you may choose not to insert any characters at all.
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".
class Solution {
public List< Boolean> camelMatch(String[] queries, String pattern) {
List< Boolean> result = new ArrayList<>();
for(int i=0; i< queries.length; i++) {
result.add(hasPattern(queries[i], pattern));
}
return result;
}
// not my algo
private boolean hasPattern(String query, String pattern) {
int j = 0;
for (int i = 0; i < query.length(); i++) {
if (j < pattern.length() && query.charAt(i) == pattern.charAt(j)) {
j++;
} else if (Character.isUpperCase(query.charAt(i))) {
return false;
}
}
return j == pattern.length();
}
}