|
| 1 | +<p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p> |
| 2 | + |
| 3 | +<p>The <em>rules</em> of a Unix-style file system are as follows:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>A single period <code>'.'</code> represents the current directory.</li> |
| 7 | + <li>A double period <code>'..'</code> represents the previous/parent directory.</li> |
| 8 | + <li>Multiple consecutive slashes such as <code>'//'</code> and <code>'///'</code> are treated as a single slash <code>'/'</code>.</li> |
| 9 | + <li>Any sequence of periods that does <strong>not match</strong> the rules above should be treated as a <strong>valid directory or</strong> <strong>file </strong><strong>name</strong>. For example, <code>'...' </code>and <code>'....'</code> are valid directory or file names.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>The simplified canonical path should follow these <em>rules</em>:</p> |
| 13 | + |
| 14 | +<ul> |
| 15 | + <li>The path must start with a single slash <code>'/'</code>.</li> |
| 16 | + <li>Directories within the path must be separated by exactly one slash <code>'/'</code>.</li> |
| 17 | + <li>The path must not end with a slash <code>'/'</code>, unless it is the root directory.</li> |
| 18 | + <li>The path must not have any single or double periods (<code>'.'</code> and <code>'..'</code>) used to denote current or parent directories.</li> |
| 19 | +</ul> |
| 20 | + |
| 21 | +<p>Return the <strong>simplified canonical path</strong>.</p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong class="example">Example 1:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">path = "/home/"</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">"/home"</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<p>The trailing slash should be removed.</p> |
| 34 | +</div> |
| 35 | + |
| 36 | +<p><strong class="example">Example 2:</strong></p> |
| 37 | + |
| 38 | +<div class="example-block"> |
| 39 | +<p><strong>Input:</strong> <span class="example-io">path = "/home//foo/"</span></p> |
| 40 | + |
| 41 | +<p><strong>Output:</strong> <span class="example-io">"/home/foo"</span></p> |
| 42 | + |
| 43 | +<p><strong>Explanation:</strong></p> |
| 44 | + |
| 45 | +<p>Multiple consecutive slashes are replaced by a single one.</p> |
| 46 | +</div> |
| 47 | + |
| 48 | +<p><strong class="example">Example 3:</strong></p> |
| 49 | + |
| 50 | +<div class="example-block"> |
| 51 | +<p><strong>Input:</strong> <span class="example-io">path = "/home/user/Documents/../Pictures"</span></p> |
| 52 | + |
| 53 | +<p><strong>Output:</strong> <span class="example-io">"/home/user/Pictures"</span></p> |
| 54 | + |
| 55 | +<p><strong>Explanation:</strong></p> |
| 56 | + |
| 57 | +<p>A double period <code>".."</code> refers to the directory up a level (the parent directory).</p> |
| 58 | +</div> |
| 59 | + |
| 60 | +<p><strong class="example">Example 4:</strong></p> |
| 61 | + |
| 62 | +<div class="example-block"> |
| 63 | +<p><strong>Input:</strong> <span class="example-io">path = "/../"</span></p> |
| 64 | + |
| 65 | +<p><strong>Output:</strong> <span class="example-io">"/"</span></p> |
| 66 | + |
| 67 | +<p><strong>Explanation:</strong></p> |
| 68 | + |
| 69 | +<p>Going one level up from the root directory is not possible.</p> |
| 70 | +</div> |
| 71 | + |
| 72 | +<p><strong class="example">Example 5:</strong></p> |
| 73 | + |
| 74 | +<div class="example-block"> |
| 75 | +<p><strong>Input:</strong> <span class="example-io">path = "/.../a/../b/c/../d/./"</span></p> |
| 76 | + |
| 77 | +<p><strong>Output:</strong> <span class="example-io">"/.../b/d"</span></p> |
| 78 | + |
| 79 | +<p><strong>Explanation:</strong></p> |
| 80 | + |
| 81 | +<p><code>"..."</code> is a valid name for a directory in this problem.</p> |
| 82 | +</div> |
| 83 | + |
| 84 | +<p> </p> |
| 85 | +<p><strong>Constraints:</strong></p> |
| 86 | + |
| 87 | +<ul> |
| 88 | + <li><code>1 <= path.length <= 3000</code></li> |
| 89 | + <li><code>path</code> consists of English letters, digits, period <code>'.'</code>, slash <code>'/'</code> or <code>'_'</code>.</li> |
| 90 | + <li><code>path</code> is a valid absolute Unix path.</li> |
| 91 | +</ul> |
0 commit comments