FastPrepLongest Common Normalized Path Suffix

Longest Common Normalized Path Suffix

ZipRecruiter logoZipRecruiter● MediumFULLTIMEOA
Learn

Problem statement

You are given an array of absolute filesystem paths, paths. Every path begins with /.

A path may contain .. parent-directory segments. Simplify every path by processing its segments from left to right: append a regular segment, and remove the most recent regular segment when .. appears. Every input path is valid.

After simplification, find the longest suffix of complete path segments shared by every path. The returned suffix must not contain .. or any other special segment.

Return the suffix with one leading /. If the paths share no trailing segment, return the empty string. For one input path, return its complete simplified path.

Function

commonNormalizedPathSuffix(paths: String[]) → String

Examples

Example 1

paths = ["/a/folder1/../folder1/a/leaf.txt","/b/folder2/../folder1/a/leaf.txt","/a/folder3/folder1/folder1/../a/leaf.txt"]return = "/folder1/a/leaf.txt"

The normalized paths end with the same three segments: folder1, a, and leaf.txt.

Example 2

paths = ["/a/folder1/../folder1/a/leaf.txt","/a/../b","/a/folder3/folder1/folder1/../a/leaf.txt"]return = ""

The second path normalizes to /b, so no trailing segment is common to all three paths.

Example 3

paths = ["/docs/draft/../final.txt"]return = "/docs/final.txt"

With one input, the complete normalized path is the longest common suffix.

Constraints

  • 1 <= paths.length <= 1000
  • Every path is valid, absolute, and begins with /.
  • Each path contains at most 200 segments.
  • The total length of all path strings is at most 200000.
  • Regular segment names are non-empty and do not equal ...

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String commonNormalizedPathSuffix(String[] paths) {
    // Write your solution here.
}
paths["/a/folder1/../folder1/a/leaf.txt","/b/folder2/../folder1/a/leaf.txt","/a/folder3/folder1/folder1/../a/leaf.txt"]
expected"/folder1/a/leaf.txt"
Checking account…