410. Route Handler Matching
Asked in
Route Handler Matching

Design a route handler that stores route patterns and matches incoming URLs in the same way as a basic web framework.

A route consists of path segments separated by /. A segment enclosed in braces is a parameter. For example, the route /products/{productId}/reviews/{reviewId} contains the parameters productId and reviewId.

A parameter matches exactly one non-empty URL segment. Static segments must match exactly and comparisons are case-sensitive. A route and a URL match only when they contain the same number of segments.

If multiple routes match a URL, choose the route containing the greatest number of static segments. If there is still a tie, choose the route that was registered earlier.

Constructor

RouteHandler

RouteHandler()

Initialize an empty route handler.

Main Methods

registerHandler

void registerHandler(String route)

Parameters

  • route: The route pattern to register.

Behavior

Store the given route pattern so that it can match future URLs.

getRouteHandler

String getRouteHandler(String url)

Parameters

  • url: The URL to match against the registered routes.

Returns

Return the best matching registered route. Return empty string "" if no route matches the URL.

Follow-up Method

getRouteHandlerWithParameters

List<String> getRouteHandlerWithParameters(String url)

Parameters

  • url: The URL to match against the registered routes.

Returns

Return a list containing the matched route followed by its extracted parameters in name=value format. Parameters must appear in their left-to-right order in the route.

Return an empty list if no route matches.

Matching Rules

  • Every route and URL starts with /.
  • A static segment matches only the same case-sensitive segment.
  • A parameter such as {itemId} matches one non-empty segment.
  • A parameter occupies an entire route segment.
  • Parameter values are returned exactly as they appear in the URL.
  • Routes containing more static segments have higher priority.
  • Registration order resolves any remaining tie.

Constraints

  • 2 ≤ route.length(), url.length() ≤ 1,000
  • Every route and URL starts with /.
  • Routes and URLs do not contain consecutive or trailing / characters.
  • Each registered route is unique.
  • Parameter names are unique within a route.
  • Parameter names contain only English letters and digits.
  • URLs do not contain parameter segments, query strings, or fragments.
  • At most 10,000 routes are registered.
  • At most 100,000 method calls are made.

Examples

Example 1

RouteHandler routeHandler = new RouteHandler()

routeHandler.registerHandler( route = "/stores/{storeId}/items/{itemId}" )

routeHandler.getRouteHandler( url = "/stores/42/items/900" )

Output: "/stores/{storeId}/items/{itemId}"

Both parameter segments match the corresponding URL segments.

Example 2

RouteHandler routeHandler = new RouteHandler()

routeHandler.registerHandler( route = "/stores/{storeId}/items/{itemId}" )

routeHandler.getRouteHandlerWithParameters( url = "/stores/42/items/900" )

Output: ["/stores/{storeId}/items/{itemId}", "storeId=42", "itemId=900"]

The matched route is returned first, followed by the extracted parameters in route order.

Example 3

RouteHandler routeHandler = new RouteHandler()

routeHandler.registerHandler( route = "/stores/{storeId}/items/{itemId}" )

routeHandler.registerHandler( route = "/stores/sale/items/{itemId}" )

routeHandler.getRouteHandler( url = "/stores/sale/items/28" )

Output: "/stores/sale/items/{itemId}"

Both routes match, but the selected route contains more static segments.

Example 4

RouteHandler routeHandler = new RouteHandler()

routeHandler.registerHandler( route = "/teams/{teamId}/members/{memberId}" )

routeHandler.getRouteHandler( url = "/teams/15/settings" )

Output: ""

The registered route and the URL have different numbers of segments, so they do not match.

Example 5

RouteHandler routeHandler = new RouteHandler()

routeHandler.registerHandler( route = "/articles/{articleId}" )

routeHandler.getRouteHandlerWithParameters( url = "/authors/73" )

Output: []

The static segment articles does not match authors.



Please use Laptop/Desktop or any other large screen to add/edit code.