-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsim2.py
More file actions
13 lines (13 loc) · 773 Bytes
/
sim2.py
File metadata and controls
13 lines (13 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
# Logic 1: 100 pass - 81% faster - Straighforward without considering a number of complex cases makes this pass. Just take care of .. or . alone. Considering /... kind of confuses the situation
class Solution:
def simplifyPath(self, path: str) -> str:
stack = [] # Container to hold the answer, should start with /
no_entries = set(["", "..", "."]) # No entries in result with respect to these values
elements = path.split("/") # Split by hashes to remove single or multiple hash
for ele in elements:
if stack and ele == "..": # indicates we should go back on dir
stack.pop()
elif ele not in no_entries:
stack.append(ele)
stack = "/" + "/".join(stack)
return stack