< Back 71. Simplify Path We're asked to simplify a Unix-style file path, basically implementing the realpath command. We're given a string containing directory names, slashes, periods, the regular. Two consecutive slashes is considered a single sash, two consecutive periods is considered a parent directory, and a single period is the current directory. This problem is best solved in Python because of its string manipulation capabilities. We split the given string using the slash as the delimiter, and then we iterate through the resulting list. We maintain a stack that's used to keep track of the directory we're currently in. If we encounter a directory name, we append it to the stack. If we encounter a period, we ignore it. If we encounter two periods, we pop the last directory from the stack. Finally, we join the directories in the stack using the slash as the delimiter, and we return the resulting string. The solution is as follows: class Solution: def simplifyPath(self, path: str) -> str: stack = [] for element in path.split("/"): if element == "..": if stack: stack.pop() elif not element or element == ".": pass else: stack.append(element) return "/" + "/".join(stack) _ Time Complexity: O(n) - We inspect all characters within the string. _ Space Complexity: O(n) - We maintain a stack of directories.