Problem Description
Given an array of query strings and a pattern string, determine for each query whether it is possible to form the query by inserting lowercase letters into the pattern without changing the order of the pattern's characters. In other words, the query must contain all characters of the pattern in order, and any extra characters inserted must be lowercase.
Key Insights
- Use two pointers: one for iterating over the query and one for the pattern.
- The characters in the pattern must appear in the query in the same order.
- Any extra characters in the query that do not match the pattern must be lowercase.
- If an uppercase letter appears in the query and it is not expected by the pattern, the query does not match.
Space and Time Complexity
Time Complexity: O(n * m) where n is the number of queries and m is the maximum length of a query. Space Complexity: O(1) extra space, aside from the output array.
Solution
For each query, iterate through each character using a pointer for the query and another pointer for the pattern. When a character in the query matches the current character of the pattern, move the pattern pointer forward. If the character does not match, it must be a lowercase letter; otherwise, the query fails the match. Once finished, ensure that all characters of the pattern have been matched and that any remaining characters in the query are lowercase. This two-pointer approach guarantees that the pattern's order is respected while allowing insertions.