Dynamic programming · Hard
A static routing tool supports a small glob language to decide whether a complete path matches a route pattern.
Match the entire path using ? for one character and * for zero or more characters.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ path:string, pattern:string }
Input rules
- Paths contain lowercase ASCII letters and /.
- Patterns additionally contain ? and *; wildcards may match /.
What each field means
path— the complete path string to match.pattern— literal characters plus?for one character and*for zero or more characters.- Both wildcards may match
/; there is no escape syntax.
Output
Return value and rules
- Return a boolean indicating whether the pattern matches the entire path.
- Matching only part of the path is not enough.
- An empty pattern matches only an empty path.
Constraints
- 0 <= path.length, pattern.length <= 2000
- No escaping, regex classes or special treatment of repeated stars.
Examples
Example 1
{
"path": "api/users",
"pattern": "api/*"
}Returns
trueExample 2
{
"path": "abc",
"pattern": "a?c"
}Returns
trueRequired complexity
O(nm) time and O(m) space