Skip to the code
cf.
+50 XP

#541 Redirect Rule With Wildcards

hard

A static site host lets you write redirect rules with wildcards. In a pattern, ? matches exactly one character (any character, including /), * matches any sequence of characters (including an empty one), and every other character matches only itself.

Write matchesRule(pattern, path) that returns true if the pattern matches the whole path, and false otherwise.

Example 1

Input: pattern = "/user/*", path = "/user/42/settings"
Output: true

* matches 42/settings.

Example 2

Input: pattern = "/p?st", path = "/posts"
Output: false

? matches one character, so the pattern only matches 5-character paths.

Constraints

  • 0 <= pattern.length, path.length <= 2000
  • path has no ? or * characters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).