bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 71. Simplify Path

71. Simplify Path

Leetcode Solutions

Using a Stack to Simplify Unix File Path

  1. Initialize an empty stack S.
  2. Split the input path by '/' to get the path components.
  3. Iterate over each component in the path. a. If the component is '.' or an empty string, continue to the next component. b. If the component is '..', pop from the stack if it is not empty. c. If the component is a valid directory name, push it onto the stack.
  4. Join all the elements in the stack with '/' to form the simplified path.
  5. Ensure the simplified path starts with '/' and does not end with '/' unless the path is just '/'.
  6. Return the simplified path.
UML Thumbnail

Using String Manipulation to Simplify Unix File Path

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR