Skip to content

Commit 3dc726e

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (40.16%), Memory - 18 MB (6.03%)
1 parent 48aa0e7 commit 3dc726e

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

0071-simplify-path/README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
<p>Given an absolute path for a Unix-style file system, which begins with a slash <code>&#39;/&#39;</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>&#39;/&#39;</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
22

3-
<p>In Unix-style file system context, a single period <code>&#39;.&#39;</code> signifies the current directory, a double period <code>&quot;..&quot;</code> denotes moving up one directory level, and multiple slashes such as <code>&quot;//&quot;</code> are interpreted as a single slash. In this problem, treat sequences of periods not covered by the previous rules (like <code>&quot;...&quot;</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>
44

5-
<p>The simplified canonical path should adhere to the following rules:</p>
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>
613

714
<ul>
8-
<li>It must start with a single slash <code>&#39;/&#39;</code>.</li>
9-
<li>Directories within the path should be separated by only one slash <code>&#39;/&#39;</code>.</li>
10-
<li>It should not end with a slash <code>&#39;/&#39;</code>, unless it&#39;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>&#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>
1219
</ul>
1320

14-
<p>Return the new path.</p>
21+
<p>Return the <strong>simplified canonical path</strong>.</p>
1522

1623
<p>&nbsp;</p>
1724
<p><strong class="example">Example 1:</strong></p>
@@ -26,8 +33,6 @@
2633
<p>The trailing slash should be removed.</p>
2734
</div>
2835

29-
<div class="example-block">&nbsp;</div>
30-
3136
<p><strong class="example">Example 2:</strong></p>
3237

3338
<div class="example-block">
@@ -49,7 +54,8 @@
4954

5055
<p><strong>Explanation:</strong></p>
5156

52-
<p>A double period <code>&quot;..&quot;</code> refers to the directory up a level.</p>
57+
<p>A double period <code>&quot;..&quot;</code> refers to the directory up a level (the parent directory).</p>
58+
</div>
5359

5460
<p><strong class="example">Example 4:</strong></p>
5561

@@ -62,7 +68,6 @@
6268

6369
<p>Going one level up from the root directory is not possible.</p>
6470
</div>
65-
</div>
6671

6772
<p><strong class="example">Example 5:</strong></p>
6873

0071-simplify-path/solution.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Approach: Using Stacks
1+
# Approach: Stacks
22

3-
# Time: O(N), N = number of characters in the original path
4-
# Space: O(N)
3+
# Time: O(n)
4+
# Space: O(n)
55

66
class Solution:
77
def simplifyPath(self, path: str) -> str:
@@ -14,8 +14,7 @@ def simplifyPath(self, path: str) -> str:
1414
elif portion == '.' or not portion:
1515
continue
1616
else:
17-
stack.append(portion)
17+
stack.append(portion)
1818

1919
return '/' + '/'.join(stack)
20-
21-
20+

0 commit comments

Comments
 (0)