Skip to content

Commit

Permalink
Create 71-Simplify-Path.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KORINZ authored Nov 30, 2022
1 parent a8550cb commit ffa05bb
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/71-Simplify-Path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def simplifyPath(self, path: str) -> str:

stack = []

for i in path.split("/"):
# if i == "/" or i == '//', it becomes '' (empty string)

if i == "..":
if stack:
stack.pop()
elif i == "." or i == '':
# skip "." or an empty string
continue
else:
stack.append(i)

res = "/" + "/".join(stack)
return res

0 comments on commit ffa05bb

Please sign in to comment.