Simplify path | February LeetCode Challenge 2021 | Day 5
logo
Manas Sinha
Developer | Designer

By Manas | 5 February 2021

February LeetCoding Challenge 2021

Day 5

SIMPLIFY PATH

PROBLEM  STATEMENT: Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path. In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names. The canonical path should have the following format:
  • The path starts with a single slash '/'.
  • Any two directories are separated by a single slash '/'.
  • The path does not end with a trailing '/'.
  • The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')
Return the simplified canonical path. Example 1
Input: path = "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.
Example 2
Input: path = "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op,
as the root level is the highest level you can go.
Example 3
Input: path = "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Example 4
Input: path = "/a/./b/../../c/"
Output: "/c"
Constraints:
  • 1 <= path.length <= 3000
  • path consists of English letters, digits, period ‘.’, slash ‘/’ or ‘_’.
  • path is a valid absolute Unix path.

Explanation

As you can probably see there are three types of directories between every two '/' 
  1. one is the actual directory (a,b,c etc)
  2. other is the '.' meaning the current directory and,
  3. ‘ ‘ empty string is basically multiple consecutive ‘/’
  4. '..' meaning the previous directory ,i.e, one-up.
This can be easily solved using stack
ALGORITHM:
  • Create a stack
  • Traverse the path string and look for the substring between every two ‘ / ‘
  • If its of type 1 add it to the stack
  • If its type 2 or 3 do nothing
  • If its type 4 pop the the top element from stack
  • Finally create a new path string using stack elements separated by ‘ / ‘

code

class Solution {
public:
    string simplifyPath(string path) {
        stack<string> st;
        if(path[0] != '/') path.insert(path.begin(),'/');
        
        string curr_dir = "";
        for(char c : path){
           if(c == '/'){
               if(curr_dir != "" && curr_dir != "." && curr_dir !="..")
                   st.push(curr_dir);
                   
               else if(curr_dir == "..")
                   if(!st.empty()) st.pop();
               
               curr_dir = "";
           }
            else curr_dir.push_back(c);
        }
        if(curr_dir != "" && curr_dir != "." && curr_dir !="..")
            st.push(curr_dir);
        else if(curr_dir == "..")
            if(!st.empty()) st.pop();
       
        string new_path = "";
        while(!st.empty()){
            new_path = "/" + st.top() + new_path;
            //cout<<"adding "<<st.top()<<endl;
            st.pop();
        }
        
        return new_path.length() ? new_path : "/";
        
    }
};

LEAVE A COMMENT

If you like the post leave a comment and share it.

Leave a Reply