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.
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".
Constraints:
1 <= pattern.length, queries.length <= 1001 <= queries[i].length <= 100queries[i] and pattern consist of English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy is like trying to spell a word using letter tiles where you are given a set of possible words. You try every possible combination of the words to see if you can form the intended word. You can think of this like the children's toy 'Bananagrams'.
Here's how the algorithm would work step-by-step:
def camel_case_matching_brute_force(queries, pattern):
results = []
for query in queries:
results.append(is_match_brute_force(query, pattern))
return results
def is_match_brute_force(query, pattern):
query_index = 0
pattern_index = 0
while query_index < len(query) and pattern_index < len(pattern):
if query[query_index] == pattern[pattern_index]:
query_index += 1
pattern_index += 1
elif query[query_index].isupper():
# If uppercase but doesn't match, this fails
return False
else:
query_index += 1
# If the pattern is not fully consumed, it is not a match.
if pattern_index != len(pattern):
return False
# Checking if rest of the query string contains uppercase chars.
while query_index < len(query):
if query[query_index].isupper():
return False
query_index += 1
return TrueThe goal is to check if a query string can be formed by characters in a pattern string, with extra lowercase characters possibly present in the pattern. The key is to walk through both strings, matching uppercase characters carefully. Mismatches of uppercase letters mean the pattern doesn't match the query.
Here's how the algorithm would work step-by-step:
def camel_case_matching(queries, pattern):
results = []
for query in queries:
pattern_index = 0
query_index = 0
matched = True
while query_index < len(query):
if pattern_index < len(pattern) and \
query[query_index] == pattern[pattern_index]:
pattern_index += 1
query_index += 1
elif query[query_index].isupper():
# Uppercase mismatch means no match
matched = False
break
else:
query_index += 1
# Check remaining query characters
if matched and pattern_index < len(pattern):
matched = False
results.append(matched)
return results| Case | How to Handle |
|---|---|
| queries or pattern is null or empty | Return an empty list if either queries or pattern is null or empty to avoid null pointer exceptions and handle invalid inputs. |
| pattern is longer than any query string | Return false immediately for that query as a camelCase subsequence cannot be longer than the string itself. |
| query string is empty but pattern is not | Return false since the query cannot match a non-empty pattern. |
| pattern contains uppercase characters that are not present in query string | The solution should correctly identify the mismatch and return false. |
| query string contains extra uppercase characters that are not part of the pattern | The solution should still return true if the pattern is a subsequence of the uppercase characters in the query. |
| Pattern matches a prefix of the query but not the entire uppercase subsequence | The solution needs to ensure all uppercase characters of the pattern are found and in the correct order within the query's uppercase subsequence. |
| Extremely long query strings | Ensure the algorithm's time complexity remains linear with respect to query length to avoid performance issues. |
| Pattern is a single uppercase character | This should be handled correctly by the subsequence matching logic, ensuring at least one uppercase character is in the query. |