|
1 | | -<p>Given an absolute path for a Unix-style file system, which begins with a slash <code>'/'</code>, transform this path into its <strong>simplified canonical path</strong>.</p> |
| 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 | 2 |
|
3 | | -<p>In Unix-style file system context, a single period <code>'.'</code> signifies the current directory, a double period <code>".."</code> denotes moving up one directory level, and multiple slashes such as <code>"//"</code> are interpreted as a single slash. In this problem, treat sequences of periods not covered by the previous rules (like <code>"..."</code>) as valid names for files or directories.</p> |
| 3 | +<p>The <em>rules</em> of a Unix-style file system are as follows:</p> |
4 | 4 |
|
5 | | -<p>The simplified canonical path should adhere to the following rules:</p> |
| 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> |
6 | 13 |
|
7 | 14 | <ul> |
8 | | - <li>It must start with a single slash <code>'/'</code>.</li> |
9 | | - <li>Directories within the path should be separated by only one slash <code>'/'</code>.</li> |
10 | | - <li>It should not end with a slash <code>'/'</code>, unless it's the root directory.</li> |
11 | | - <li>It should exclude any single or double periods used to denote current or parent directories.</li> |
| 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> |
12 | 19 | </ul> |
13 | 20 |
|
14 | | -<p>Return the new path.</p> |
| 21 | +<p>Return the <strong>simplified canonical path</strong>.</p> |
15 | 22 |
|
16 | 23 | <p> </p> |
17 | 24 | <p><strong class="example">Example 1:</strong></p> |
|
26 | 33 | <p>The trailing slash should be removed.</p> |
27 | 34 | </div> |
28 | 35 |
|
29 | | -<div class="example-block"> </div> |
30 | | - |
31 | 36 | <p><strong class="example">Example 2:</strong></p> |
32 | 37 |
|
33 | 38 | <div class="example-block"> |
|
49 | 54 |
|
50 | 55 | <p><strong>Explanation:</strong></p> |
51 | 56 |
|
52 | | -<p>A double period <code>".."</code> refers to the directory up a level.</p> |
| 57 | +<p>A double period <code>".."</code> refers to the directory up a level (the parent directory).</p> |
| 58 | +</div> |
53 | 59 |
|
54 | 60 | <p><strong class="example">Example 4:</strong></p> |
55 | 61 |
|
|
62 | 68 |
|
63 | 69 | <p>Going one level up from the root directory is not possible.</p> |
64 | 70 | </div> |
65 | | -</div> |
66 | 71 |
|
67 | 72 | <p><strong class="example">Example 5:</strong></p> |
68 | 73 |
|
|
0 commit comments