Simplify Path - Complete Solution Guide
Simplify Path is LeetCode problem 71, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an absolute path for a Unix-style file system, which always begins with a slash '/' . Your task is to transform this absolute path into its simplified canonical path . The rules of a Unix-style file system are as follows: A single period '.' represents the current directory. A double period '..' represents the previous/parent directory. Multiple consecutive slashes such as '//' and '///' are treated as a single slash '/' . Any sequence of periods that does not match the rules above
Detailed Explanation
The problem asks us to simplify a given absolute Unix-style file system path. The simplified path must adhere to specific rules: it should start with a single slash, directories should be separated by exactly one slash, and it shouldn't end with a slash unless it represents the root directory. Single periods ('.') represent the current directory and should be ignored. Double periods ('..') represent the parent directory, so we need to go up one level in the directory structure. Multiple consecutive slashes should be treated as a single slash. The input is a string representing the absolute path, and the output is the simplified canonical path.
Solution Approach
The solution utilizes a stack to maintain the directory hierarchy. The input path is first split into components using '/' as a delimiter. Each component is then processed: If it's '..', we pop an element from the stack (if the stack is not empty) to go up one level. If it's a directory name (not empty and not '.' or '..'), we push it onto the stack. Finally, we construct the simplified path by joining the elements in the stack with '/' and prepending a leading '/'. Special handling is required to ensure the output is '/' if the stack is empty (root directory).
Step-by-Step Algorithm
- Step 1: Split the input path string into a list of components using the '/' character as a delimiter.
- Step 2: Initialize an empty stack to store the directory names.
- Step 3: Iterate through each component in the list.
- Step 4: If the component is '..', check if the stack is not empty. If it's not empty, pop the top element from the stack (representing moving to the parent directory).
- Step 5: If the component is not empty and not equal to '.' and not equal to '..', push the component onto the stack (representing a valid directory).
- Step 6: After processing all components, construct the simplified path by joining the elements in the stack with '/' as a delimiter.
- Step 7: Prepend a '/' to the resulting string to ensure it's an absolute path.
- Step 8: If the stack is empty after processing (meaning we navigated back to the root directory or started at the root), return "/". Otherwise, return the constructed path.
Key Insights
- Insight 1: Using a stack is crucial for keeping track of the directory structure and easily navigating up the hierarchy when encountering '..'.
- Insight 2: Splitting the path by '/' allows us to process each directory component individually.
- Insight 3: Handling edge cases like consecutive slashes, single periods, double periods, and ensuring the final path format is correct is essential for a robust solution.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String, Stack.
Companies
Asked at: Adobe, Amazon, Apple, Bloomberg, Capital One, Citadel, Gojek, Grab, Harness, Meta, Microsoft, OpenAI, Patreon, Roku, Snowflake, Tesla, TikTok, Tinkoff, Upstart, Visa, Yahoo, Yandex.