A deployment tool resolves file references relative to a current base directory. You are given a base directory (an absolute path starting with /) and a target path. Produce the canonical absolute path the target refers to:
/, it is absolute and is resolved from the root, ignoring the base.. means the current directory (drop it), .. means the parent directory (remove the previous real name; at the root it has no effect), and repeated slashes collapse./ and, unless it is the root, has no trailing /. The root itself is /.Directory names between slashes consist of lowercase letters and digits only.
Line 1: the base directory (an absolute path beginning with /).
Line 2: the target path.
A single line: the canonical absolute resolved path.
/. Both lines use only /, lowercase letters, digits, and . characters.Example 1
Input
/home/user ../admin/./config
Expected
/home/admin/config
Explanation
The target is relative, so start from home, user; then .. removes user, admin is added, . is dropped, config is added, giving /home/admin/config.
Example 2
Input
/var/log /etc/nginx
Expected
/etc/nginx
Explanation
The target starts with /, so it is absolute and the base is ignored; the canonical result is /etc/nginx.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →