Path prefix routing

Write codeEasy15 min

Your task

Given a list of URL paths, compute their longest common path prefix for routing purposes, matching on whole path segments (not characters). Each path starts with '/'. Return the common prefix as a path string starting with '/' (e.g. '/api/v1'); if the only common ancestor is the root, return '/'. An empty list returns '/'. A single path returns that path.

Examples

common_path_prefix(paths: list[str]) -> str
Example 1
Inputcommon_path_prefix(["/api/v1/users", "/api/v1/orders", "/api/v1/users/5"])
Output"/api/v1"