HTTP Route Status Counts
MediumParsing · Stripe
Simulate a small HTTP router and count the status codes it returns.
routes is a list of [method, pattern, status] in priority order. requests is a list of [method, path]. Paths and patterns start with / and are split into segments on /.
A pattern matches a path when both have the same number of segments and each pattern segment either:
- equals the path segment exactly (case-sensitive), or
- is a parameter written as
{name}, which matches any non-empty path segment.
So /v1/charges/{id} matches /v1/charges/ch_1 but not /v1/charges/, /v1/charges, or /v1/charges/ch_1/refunds.
For each request:
- If some route matches both the method and the path, respond with the status of the first such route in
routes. - Otherwise, if some route matches the path with a different method, respond
405. - Otherwise respond
404.
Return [status, count] pairs for every status that occurred, sorted by status ascending.
Example 1
- Input:
- routes = [["GET", "/v1/charges/{id}", 200], ["POST", "/v1/charges/{id}/capture", 202], ["GET", "/v1/balance", 200]], requests = [["GET", "/v1/charges/ch_42"], ["POST", "/v1/charges/ch_42"], ["POST", "/v1/charges/ch_7/capture"], ["GET", "/v1/charges/ch_7/capture/now"], ["GET", "/v1/balance"]]
- Output:
- [[200, 2], [202, 1], [404, 1], [405, 1]]
- Explanation
GET /v1/charges/ch_42andGET /v1/balancereturn 200, and the capture request matches the parameterized POST route for 202.POST /v1/charges/ch_42matches the{id}pattern only for GET, so it is 405. The last path has one segment too many for every route, so it is 404.
Example 2
- Input:
- routes = [["GET", "/v1/charges/{id}", 200], ["GET", "/v1/charges/search", 299]], requests = [["GET", "/v1/charges/search"], ["GET", "/v1/charges/ch_1"]]
- Output:
- [[200, 2]]
- Explanation
- Both routes match
/v1/charges/search, and the parameterized route is listed first, so both requests return 200.
Example 3
- Input:
- routes = [["DELETE", "/v1/customers/{id}", 204]], requests = [["DELETE", "/v1/customers/"], ["GET", "/v1/customers/cus_1"], ["DELETE", "/v1/customers/cus_1"]]
- Output:
- [[204, 1], [404, 1], [405, 1]]
- Explanation
/v1/customers/ends with an empty segment, which{id}cannot match, so it is 404.GETon a real id matches the path with the wrong method (405), and the DELETE succeeds with 204.
Constraints
0 <= routes.length <= 2001 <= requests.length <= 10^4- Every path and pattern starts with
/and has at most 10 segments - Methods are uppercase strings and
100 <= status <= 599