My Java Version


#1
    Deque<String> stack = new LinkedList<>();
    Set<String> hSet = new HashSet<>(Arrays.asList("..",".",""));
    for(String dir : path.split("/")){
        if(dir.equals("..") && !stack.isEmpty()) stack.pop();
        else if(!hSet.contains(dir)) stack.push(dir);
    }
    String res = "";
    for(String dir : stack) res = "/" + dir + res;
    return res.isEmpty() ? "/" : res;