Advertisement
smj007

Simplify path

Aug 6th, 2024
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. class Solution:
  2.     def simplifyPath(self, path: str) -> str:
  3.         stack = []
  4.         running_string = ""
  5.  
  6.         for c in path + "/":
  7.             if c == "/":
  8.                 if running_string == ".." and len(stack) > 0:
  9.                     stack.pop()
  10.                 if running_string not in [".", "", ".."]:
  11.                     stack.append(running_string)
  12.                 running_string = ""
  13.             else:
  14.                 running_string += c
  15.  
  16.         return "/" + "/".join(stack)
  17.  
  18. class Solution:
  19.     def simplifyPath(self, path: str) -> str:
  20.         stack = []
  21.  
  22.         for part in path.split("/"):
  23.             if part == "..":
  24.                 if stack:
  25.                     stack.pop()
  26.             elif part not in [".", ""]:
  27.                 stack.append(part)
  28.  
  29.         return "/" + "/".join(stack)
  30.  
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement