Skip to content

Commit 967bb3f

Browse files
committed
LC 71. Simplify Path (Phython Stack)
1 parent bc16091 commit 967bb3f

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
7373
| [64. Minimum Path Sum][lc64] | 🟠 Medium | [![rust](res/rs.png)][lc64rs] |
7474
| [66. Plus One][lc66] | 🟢 Easy | [![python](res/py.png)][lc66py] |
7575
| [67. Add Binary][lc67] | 🟢 Easy | [![python](res/py.png)][lc67py] [![rust](res/rs.png)][lc67rs] |
76-
| [70. Climbing Stairs][lc70] | 🟢 Easy | [![python](res/py.png)](leetcode/climbing-stairs.py) |
76+
| [70. Climbing Stairs][lc70] | 🟢 Easy | [![python](res/py.png)][lc70py] |
77+
| [71. Simplify Path][lc71] | 🟠 Medium | [![python](res/py.png)][lc71py] |
7778
| [72. Edit Distance][lc72] | 🔴 Hard | [![python](res/py.png)][lc72py] [![rust](res/rs.png)][lc72rs] |
7879
| [73. Set Matrix Zeroes][lc73] | 🟠 Medium | [![python](res/py.png)][lc73py] |
7980
| [74. Search a 2D Matrix][lc74] | 🟠 Medium | [![python](res/py.png)][lc74py] |
@@ -595,6 +596,9 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
595596
[lc67py]: leetcode/add-binary.py
596597
[lc67rs]: leetcode/add-binary.rs
597598
[lc70]: https://leetcode.com/problems/climbing-stairs/
599+
[lc70py]: leetcode/climbing-stairs.py
600+
[lc71]: https://leetcode.com/problems/simplify-path/
601+
[lc71py]: leetcode/simplify-path.py
598602
[lc72]: https://leetcode.com/problems/edit-distance/
599603
[lc72py]: leetcode/edit-distance.py
600604
[lc72rs]: leetcode/edit-distance.rs

leetcode/simplify-path.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 71. Simplify Path
2+
# 🟠 Medium
3+
#
4+
# https://leetcode.com/problems/simplify-path/
5+
#
6+
# Tags: String - Stack
7+
8+
import re
9+
import timeit
10+
11+
12+
# First merge contiguous directory separators into one, then split by
13+
# directory separators. Iterate over the resulting directories or ".."
14+
# elements, when we see "." we don't do anything, when we see a ".." we
15+
# "travel" one directory up by popping the last element on the stack,
16+
# anything else we consider a directory name and we push into the stack.
17+
#
18+
# Time complexity: O(n) - Cleaning and tokenizing the input needs to
19+
# visit each character of the input path.
20+
# Space complexity: O(n) - The stack can have as many characters as the
21+
# input.
22+
#
23+
# Runtime 35 ms Beats 58.39%
24+
# Memory 13.8 MB Beats 69.41%
25+
class Solution:
26+
def simplifyPath(self, path: str) -> str:
27+
# Step 1 combine multiple '////' into '/'
28+
clean = re.sub(r"/+", "/", path)
29+
# Step 2 split into directory names.
30+
# https://stackoverflow.com/a/34844548/2557030
31+
tokens = [token for token in clean.split("/") if token]
32+
# Use a stack to store directory names
33+
stack = []
34+
for token in tokens:
35+
if token == ".":
36+
continue
37+
if token == "..":
38+
if stack:
39+
stack.pop()
40+
continue
41+
stack.append(token)
42+
return "/" + "/".join(stack)
43+
44+
45+
def test():
46+
executors = [Solution]
47+
tests = [
48+
["/../", "/"],
49+
["/a/..", "/"],
50+
["/a/../", "/"],
51+
["/home/", "/home"],
52+
["/abc/...", "/abc/..."],
53+
["/a/./b/../../c/", "/c"],
54+
["/a/../.././../../", "/"],
55+
["/../../../../../a", "/a"],
56+
["/home//foo/", "/home/foo"],
57+
["/a//b//c//////d", "/a/b/c/d"],
58+
["/a/./b/./c/./d/", "/a/b/c/d"],
59+
]
60+
for executor in executors:
61+
start = timeit.default_timer()
62+
for _ in range(1):
63+
for col, t in enumerate(tests):
64+
sol = executor()
65+
result = sol.simplifyPath(t[0])
66+
exp = t[1]
67+
assert result == exp, (
68+
f"\033[93m» {result} <> {exp}\033[91m for"
69+
+ f" test {col} using \033[1m{executor.__name__}"
70+
)
71+
stop = timeit.default_timer()
72+
used = str(round(stop - start, 5))
73+
cols = "{0:20}{1:10}{2:10}"
74+
res = cols.format(executor.__name__, used, "seconds")
75+
print(f"\033[92m» {res}\033[0m")
76+
77+
78+
test()

0 commit comments

Comments
 (0)