From 47ae15fd37b6197fd6d2e0a469eb7cf036d2f6e1 Mon Sep 17 00:00:00 2001 From: Eric Kim <48634064+hyeonukim@users.noreply.github.com> Date: Tue, 14 Jan 2025 20:32:25 -0800 Subject: [PATCH] Create 2025-01-14-Leetcode-71.md --- _posts/2025-01-14-Leetcode-71.md | 107 +++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 _posts/2025-01-14-Leetcode-71.md diff --git a/_posts/2025-01-14-Leetcode-71.md b/_posts/2025-01-14-Leetcode-71.md new file mode 100644 index 0000000..23b3857 --- /dev/null +++ b/_posts/2025-01-14-Leetcode-71.md @@ -0,0 +1,107 @@ +--- +title: Leetcode 71. Simplify Path +description: Explanation for Leetcode 71 - Simplify Path, and its solution in Python. +date: 2025-01-14 +categories: [Leetcode, Stack, Medium] +tags: [Leetcode, Python, Study, Stack, Medium] +math: true +--- + +## Problem +[Leetcode 71 - Simplify Path](https://leetcode.com/problems/simplify-path/description/) + +Example: +``` +Input: path = "/home/" +Output: "/home" +Explanation: The trailing slash should be removed. + +Input: path = "/home//foo/" +Output: "/home/foo" +Explanation: Multiple consecutive slashes are replaced by a single one. + +Input: path = "/home/user/Documents/../Pictures" +Output: "/home/user/Pictures" +Explanation: A double period ".." refers to the directory up a level (the parent directory). + +Input: path = "/../" +Output: "/" +Explanation: Going one level up from the root directory is not possible. + +Input: path = "/.../a/../b/c/../d/./" +Output: "/.../b/d" +Explanation: "..." is a valid name for a directory in this problem. +``` + +## Approach + +As explained in the description of the problem, + +- a single period '.' represents the current directory + - we can just ignore when we encounter '.' +- a double period '..' represents the previous/parent directory + - we can pop the stack if len(stack) > 0 +- multiple consecutive slashes such as '//' or '///' are treated as single slash '/' + - we can ignore this since .split('/') will solve this +- any sequence of periods that does not match the rules above should be treated as a valid directory or file name. + - append it to stack + +When this is all done, we can join the stack with '/'. Since we must also start with '/' for return value, we have to add '/' + +Visualization of the Approach: +``` +path = "/home/user/Documents/../Pictures" + +After doing split operation +path = ['', 'home', 'user', 'Documents', '..', 'Pictures'] +stack = [] + +Since '' is empty, we ignore and continue +stack = [] + +Since 'home' is a valid name for directory add it to stack +stack = ['home'] + +Since 'user' is a valid name for directory add it to stack +stack = ['home', 'user'] + +Since 'Documents' is a valid name for directory add it to stack +stack = ['home', 'user', 'Documents'] + +Since '..', we should pop one from stack if len(stack) > 0 +stack = ['home', 'user'] + +Since 'Pictures' is a valid name for directory add it to stack +stack = ['home', 'user', 'Pictures'] + +When we return we must add '/' first then join the list with '/' +Thus, res = '/home/user/Pictures' +``` + +Here is the Python code for the solution: +```python +class Solution: + def simplifyPath(self, path: str) -> str: + stack = [] + path = path.split('/') + + for p in path: + if p == '': + continue + elif p == '.': + continue + elif p == '..': + if len(stack) == 0: + continue + else: + stack.pop() + else: + stack.append(p) + + return '/' + '/'.join(stack) +``` +## Time Complexity and Space Complexity + +Time Complexity: $O(n)$ + +Space Complexity: $O(n)$ \ No newline at end of file