FastPrepLongest Common File-System Path Prefix

Longest Common File-System Path Prefix

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

Problem statement

Every input is a canonical absolute Unix path: it starts with /, has no repeated separators, and has no trailing separator unless it is the root.

Return the longest path prefix whose complete components are shared by every input. Return / when no named component is common, and return an empty string when paths is empty.

Function

commonPathPrefix(paths: String[]) → String

Examples

Example 1

paths = ["/a/b/c","/a/b/d","/a/b"]return = "/a/b"

Only complete common components are retained.

Example 2

paths = ["/alpha","/beta"]return = "/"

Different first components share only the root.

Constraints

  • 0 <= paths.length <= 10000
  • The total path length is at most 200000.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String commonPathPrefix(String[] paths) {
    // Write your code here.
}
paths["/a/b/c","/a/b/d","/a/b"]
expected"/a/b"
Checking account…