Find a File in a Directory Snapshot
Problem statement
For this exercise, assume a directory is supplied as a snapshot of its files. rootDir is its absolute path, and every value in relativePaths is the relative path of one regular file below that root, including files in nested directories.
Find files whose final path segment equals fileName exactly. Return the lexicographically smallest matching absolute path. If no file matches, return the empty string.
Join the root and relative path with exactly one slash. Matching is case-sensitive and compares the entire filename, including its extension. No real filesystem access is required.
Function
findAbsolutePath(rootDir: String, relativePaths: String[], fileName: String) → StringExamples
Example 1
rootDir = "/work"relativePaths = ["z/log.txt","a/log.txt","notes.md"]fileName = "log.txt"return = "/work/a/log.txt"Both matching files end in log.txt; /work/a/log.txt is lexicographically smaller.
Example 2
rootDir = "/"relativePaths = ["main.py"]fileName = "main.py"return = "/main.py"Joining the root with the relative file gives /main.py.
Example 3
rootDir = "/data"relativePaths = ["x/a.txt"]fileName = "b.txt"return = ""No basename equals b.txt.
Constraints
0 <= relativePaths.length <= 10^4; paths are unique.- The root is
/or a valid absolute path without a trailing slash. Relative paths have no leading or trailing slash. - Segments contain lowercase English letters, digits, dots, underscores or hyphens, and are never
.or... - Each path contains at most
200characters.fileNameis one nonempty valid segment.