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 pattern so that it equals the query. You may insert each character at any position and you may not insert any characters.
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.Problem Overview: You receive a list of query strings and a camelCase pattern. A query matches the pattern if you can insert lowercase letters into the pattern to form the query without changing the order of the pattern’s characters. Any extra uppercase letter that does not appear in the pattern immediately invalidates the match.
The challenge is verifying that uppercase characters align exactly with the pattern while allowing extra lowercase characters anywhere. Each query must be checked independently, which makes careful character comparison the key part of the algorithm.
Approach 1: Two-Pointer Technique (O(q * n) time, O(1) space)
This approach scans each query and the pattern using two pointers. One pointer walks through the query while the other tracks the current position in the pattern. When characters match, both pointers advance. If the query character is lowercase and doesn't match, it can be skipped. If it is uppercase and doesn't match the pattern, the query immediately fails because uppercase letters cannot be inserted arbitrarily.
The key insight: uppercase letters must match exactly and in order, while lowercase letters act as "fillers" that can be ignored. You iterate through the query once, making the check linear for each query. This solution uses only constant extra memory and is typically the expected interview solution when working with two pointers and string processing.
Approach 2: Regular Expression Matching (O(q * n) time, O(n) space)
The pattern can be converted into a regular expression that enforces the camelCase constraints. For example, between every pattern character you allow any number of lowercase letters using [a-z]*. The regex becomes something like ^[a-z]*P[a-z]*a[a-z]*t[a-z]*... depending on the pattern characters. Each query is then tested against this compiled expression.
This approach is concise in languages with strong regex support such as Python or JavaScript. However, it introduces regex engine overhead and requires building the pattern dynamically. While the complexity remains linear relative to query length, the constant factors are higher than the manual scan.
Recommended for interviews: The Two-Pointer Technique is the expected solution. It demonstrates control over string traversal, conditional checks for uppercase characters, and constant-space design. Regex can be a quick implementation in production code, but interviewers generally prefer the explicit pointer-based logic because it clearly shows how you enforce the camelCase rules. The problem mainly tests string processing skills and pattern validation using ideas common in array traversal and pointer scanning.
This approach uses a two-pointer technique to check if each query can match the pattern.
Traverse over both the query and pattern with a pointer for each. If the characters match, move both pointers; if not, just move the pointer of the query. The key is to ensure all uppercase letters in the query are matched by the pattern in order.
We define a helper function matches that checks whether a single query matches the pattern using two pointers. The main function camelMatch iterates over all the queries and uses matches to generate the result boolean array.
Time Complexity: O(n * m), where n is the number of queries and m is the average length of the queries.
Space Complexity: O(n), for storing the results.
This approach involves creating regular expressions from the patterns and matching them against each query directly.
Construct a regex pattern by translating the given pattern where each uppercase letter is preceded by a regex for any lowercase letters. We then apply this regex to each query.
In JavaScript, convert the pattern into a regex pattern by appending [a-z]* after each character, then match it against each query using the test method.
JavaScript
Python
Time Complexity: Depends on regex engine but generally O(n * m), where n is the number of queries and m is the average length of the queries.
Space Complexity: O(n), for storing results.
We can traverse every string in queries and check whether it matches pattern or not. If it matches, we add true to the answer array, otherwise we add false.
Next, we implement a function check(s, t) to check whether the string s matches the string t.
We can use two pointers i and j to traverse the two strings. If the characters pointed to by i and j are not the same and s[i] is a lowercase letter, then we move the pointer i to the next position.
If the pointer i has reached the end of the string s or the characters pointed to by i and j are not the same, we return false. Otherwise, we move both pointers i and j to the next position. When the pointer j reaches the end of the string t, we need to check if the remaining characters in the string s are all lowercase letters. If so, we return true, otherwise we return false.
Time complexity (n times m), where n and m are the length of the array queries and the string pattern respectively.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Two-Pointer Technique | Time Complexity: O(n * m), where n is the number of queries and m is the average length of the queries. Space Complexity: O(n), for storing the results. |
| Regular Expression Matching | Time Complexity: Depends on regex engine but generally O(n * m), where n is the number of queries and m is the average length of the queries. Space Complexity: O(n), for storing results. |
| Two Pointers | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-Pointer Technique | O(q * n) | O(1) | Best general solution; efficient for iterating through each query and validating uppercase alignment |
| Regular Expression Matching | O(q * n) | O(n) | Useful when regex libraries are available and quick pattern construction is preferred |
1023. Camelcase Matching | LEETCODE MEDIUM | STRING MATCHING • code Explainer • 2,114 views views
Watch 9 more video solutions →Practice Camelcase Matching with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor