https://leetcode.com/problems/camelcase-matching/
X. https://leetcode.com/problems/camelcase-matching/discuss/270024/Java-4-liner-and-2-liner-regex
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 <= queries.length <= 100
1 <= queries[i].length <= 100
1 <= pattern.length <= 100
- All strings consists only of lower and upper case English letters
For each string, macth it with the pattern and pass the result.
The match process uses i for query pointer and j for pattern pointer, each iteration;
- If current char query[i] matches pattern[j], increase pattern pointer;
- if does not match and query[i] is lowercase, keep going;
- if does not match and query[i] is captalized, we should return false.
If this pattern matches, j should equal length of pattern at the end.
Hope this helps.
public List<Boolean> camelMatch(String[] queries, String pattern) {
List<Boolean> res = new ArrayList<>();
char[] patternArr = pattern.toCharArray();
for (String query : queries) {
boolean isMatch = match(query.toCharArray(), patternArr);
res.add(isMatch);
}
return res;
}
private boolean match(char[] queryArr, char[] patternArr) {
int j = 0;
for (int i = 0; i < queryArr.length; i++) {
if (j < patternArr.length && queryArr[i] == patternArr[j]) {
j++;
} else if (queryArr[i] >= 'A' && queryArr[i] <= 'Z') {
return false;
}
}
return j == patternArr.length;
}
X. https://leetcode.com/problems/camelcase-matching/discuss/270024/Java-4-liner-and-2-liner-regex
According to the problem description, insert
[a-z]*
before & after each char in pattern
, then use regular expression to match the new pattern. public List<Boolean> camelMatch(String[] queries, String pattern) {
String newPattern = "[a-z]*" + String.join("[a-z]*", pattern.split("")) + "[a-z]*";
List<Boolean> ans = new ArrayList<>();
for (String q : queries) { ans.add(q.matches(newPattern)); }
return ans;
}
Using java 8 stream, the above can be rewritten in 2 lines:
public List<Boolean> camelMatch(String[] queries, String pattern) {
String newPattern = "[a-z]*" + String.join("[a-z]*", pattern.split("")) + "[a-z]*";
return Arrays.stream(queries).map(q -> q.matches(newPattern)).collect(Collectors.toList());
}