Shared path root: hints, no solution
Easy · Tries
A web gateway stores route paths such as /api/v1/users. Every path starts with a slash and has segments separated by single slashes, with no trailing slash. The root path is a lone slash and has no segments.
Given a list of paths, return the longest leading run of whole segments that all of them share, written as a path that starts with a slash. Compare whole segments, not characters: /app/user and /app/users share only /app. If they share no segment at all, return a lone slash.
Examples
- Input
- paths = ["/api/v1/users", "/api/v1/orders", "/api/v1/users/42"]
- Output
- "/api/v1"
- Note
- All three paths begin with the segments api and v1, and then differ.
- Input
- paths = ["/shop/cart", "/blog/post"]
- Output
- "/"
- Note
- The first segments shop and blog differ, so only the lone slash is shared.
- Input
- paths = ["/docs/intro/setup"]
- Output
- "/docs/intro/setup"
- Note
- A single path shares every segment with itself.
Constraints
- 1 <= paths.length <= 500
- Each path is at most 80 characters and has at most 12 segments
- Segments are non-empty and use lowercase letters, digits, hyphens and dots
Hint 1: a nudge
If two paths agree on some leading characters, is that always a shared route? Try /app/user and /app/users and decide what the answer should be.
Unlock the full hints and timed practice with a pass
The second and third hints and the timed, runner-checked practice are part of a pass.