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 <= 100
1 <= queries[i].length <= 100
queries[i]
and pattern
consist of English letters.This problem asks to determine if a given query string can be generated from a pattern string by inserting lowercase letters. The core idea is to efficiently compare the query strings with the pattern string, checking for matches while allowing for insertions of lowercase characters. The most efficient approach is using a two-pointer technique.
The solution uses a helper function check(s, t)
where s
is the query string and t
is the pattern string. Two pointers, i
and j
, iterate through s
and t
respectively.
Iteration: The outer loop iterates through the pattern string (t
). The inner loop skips lowercase characters in the query string (s
) that don't match the current character in the pattern.
Mismatch Handling: If a mismatch occurs (the character at s[i]
doesn't equal t[j]
, and s[i]
isn't a lowercase character that can be skipped), the function immediately returns false
indicating no match.
Lowercase Skipping: If s[i]
is a lowercase character and doesn't match t[j]
, the inner loop advances i
to skip over these characters.
Match Advancement: When a match is found (s[i] == t[j]
), both pointers i
and j
are incremented to proceed to the next characters.
End of Pattern: If the end of the pattern is reached (j == n
), the remaining characters in s
must be lowercase to constitute a valid match. The code checks this condition, returning true
if all remaining characters in s
are lowercase, and false
otherwise.
Main Function: The main function iterates through each query string in the queries
array and applies the check
function to determine the match with the given pattern. The results are stored in a boolean array and returned.
Time Complexity: O(m*n), where m is the total length of all query strings and n is the length of the pattern string. This is because the check
function has a time complexity of O(len(s)), and it's called for each query string.
Space Complexity: O(1), as the algorithm uses a constant amount of extra space regardless of the input size.
The code examples in the problem description demonstrate this two-pointer approach in different programming languages. Each example directly implements the check
function as described above and utilizes it within a main loop to process the input queries
. The comments within the code clarify each step of the process. The core logic remains consistent across all the provided languages.