Wednesday, May 14, 2014

LeetCode: Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

Corner Cases:

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".

  • We can use a stack to help deal with the '/..' backwards case.

    C++ Code:

    /*
     * func: simplify_path
     * goal: simplify input absolute path
     * @param path: input path
     * return: simplified path
     */
    string simplify_path(const string &path){
        string result = "";
        string dir = "";
        deque<string> path_helper;
        for(const char &ch : path){
            if(ch != '/'){
                dir += ch;
            }else{
                //If current char is '/' and previous char is also '/'
                //Skip this one
                if(dir == "/"){
                    continue;
                }else if(dir == "/."){
                    //If previous dir is '/.', nothing changed, continue to read next directory
                    dir = "/";
                }else if(dir == "/.."){
                    //If previous dir is '/..', pop one from stack if there exists
                    if(!path_helper.empty()){
                        path_helper.pop_back();
                    }
                    dir = "/";
                }else{
                    //If previous dir is not a special case, push back it to stack, and read the next one
                    if(dir != "")
                        path_helper.emplace_back(dir);
                    dir = "/";
                }
            }
        }
        //If the last one is '/..', pop if available
        if(dir == "/.."){
            if(!path_helper.empty()){
                path_helper.pop_back();
            }
        }else if(dir != "/" && dir != "/."){
            //If the last one is a normal one, push back it to the stack
            path_helper.emplace_back(dir);
        }
        
        //If there is nothing in the stack, return root directory
        if(path_helper.empty()){
            return "/";
        }
        
        auto it = path_helper.begin();
        while(it != path_helper.end()){
            result += *it++;
        }
        
        return result;
    }
    

    Python Code:

    # func: simplify the input absolute path
    # @param path: input path
    # @return: simplified path
    def simplify_path(path):
        dirs = path.split("/")
        stack = []
    
        for directory in dirs:
            if not directory or directory == '.':
                continue
            elif directory == '..':
                if stack:
                    stack = stack[:-1]
            else:
                stack.append(directory)
    
        if not stack:
            return '/'
        else:
            result = ""
            for directory in stack:
                result += "/" + directory
    
            return result
    

    No comments:

    Post a Comment