Skip to content

Commit afb2fae

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 19.3 MB (76.27%)
1 parent 1992243 commit afb2fae

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>&#39;/&#39;</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>&#39;.&#39;</code> represents the current directory.</li>
7+
<li>A double period <code>&#39;..&#39;</code> represents the previous/parent directory.</li>
8+
<li>Multiple consecutive slashes such as <code>&#39;//&#39;</code> and <code>&#39;///&#39;</code> are treated as a single slash <code>&#39;/&#39;</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>&#39;...&#39; </code>and <code>&#39;....&#39;</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>&#39;/&#39;</code>.</li>
16+
<li>Directories within the path must be separated by exactly one slash <code>&#39;/&#39;</code>.</li>
17+
<li>The path must not end with a slash <code>&#39;/&#39;</code>, unless it is the root directory.</li>
18+
<li>The path must not have any single or double periods (<code>&#39;.&#39;</code> and <code>&#39;..&#39;</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>&nbsp;</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 = &quot;/home/&quot;</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">&quot;/home&quot;</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 = &quot;/home//foo/&quot;</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">&quot;/home/foo&quot;</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 = &quot;/home/user/Documents/../Pictures&quot;</span></p>
52+
53+
<p><strong>Output:</strong> <span class="example-io">&quot;/home/user/Pictures&quot;</span></p>
54+
55+
<p><strong>Explanation:</strong></p>
56+
57+
<p>A double period <code>&quot;..&quot;</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 = &quot;/../&quot;</span></p>
64+
65+
<p><strong>Output:</strong> <span class="example-io">&quot;/&quot;</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 = &quot;/.../a/../b/c/../d/./&quot;</span></p>
76+
77+
<p><strong>Output:</strong> <span class="example-io">&quot;/.../b/d&quot;</span></p>
78+
79+
<p><strong>Explanation:</strong></p>
80+
81+
<p><code>&quot;...&quot;</code> is a valid name for a directory in this problem.</p>
82+
</div>
83+
84+
<p>&nbsp;</p>
85+
<p><strong>Constraints:</strong></p>
86+
87+
<ul>
88+
<li><code>1 &lt;= path.length &lt;= 3000</code></li>
89+
<li><code>path</code> consists of English letters, digits, period <code>&#39;.&#39;</code>, slash <code>&#39;/&#39;</code> or <code>&#39;_&#39;</code>.</li>
90+
<li><code>path</code> is a valid absolute Unix path.</li>
91+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def simplifyPath(self, path: str) -> str:
3+
dirs = path.split('/')
4+
newDirs = []
5+
for d in dirs:
6+
if not d or d.isspace():
7+
continue
8+
elif d == '.':
9+
continue
10+
elif d == '..':
11+
if len(newDirs):
12+
newDirs.pop()
13+
else:
14+
newDirs.append(d)
15+
16+
return '/' + '/'.join(newDirs)

0 commit comments

Comments
 (0)