71. Simplify Path
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 47% Topics: String, Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
# @param path, a string
# @return a string
def simplifyPath(self, path):
stack, tokens = [], path.split("/")
for token in tokens:
if token == ".." and stack:
stack.pop()
elif token != ".." and token != "." and token:
stack.append(token)
return "/" + "/".join(stack)
Solution from kamyu104/LeetCode-Solutions · MIT