-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy path222.py
36 lines (25 loc) · 739 Bytes
/
222.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Problem:
Given an absolute pathname that may have . or .. as part of it, return the shortest
standardized path.
For example, given /usr/bin/../bin/./scripts/../, return /usr/bin/.
"""
from DataStructures.Stack import Stack
def get_shortest_standardized_path(path: str) -> str:
path_list = path.split("/")
stack = Stack()
for curr_directory in path_list:
if curr_directory == ".":
continue
elif curr_directory == "..":
stack.pop()
else:
stack.push(curr_directory)
return "/".join(stack)
if __name__ == "__main__":
print(get_shortest_standardized_path("/usr/bin/../bin/./scripts/../"))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""