public String simplifyPath(String path) {
        if (path == null || path.isEmpty()) return "";
        String[] dirs = path.split("/");
        Stack<String> s = new Stack<String>();
        for (String dir : dirs) {
            if (dir.isEmpty() || dir.equals(".")) {
                continue;
            } else if (dir.equals("..")) {
                if (!s.isEmpty()) s.pop();
            } else {
                s.push(dir);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!s.isEmpty()) sb.insert(0, s.pop()).insert(0, '/');
        return sb.length() == 0 ? "/" : sb.toString();
    }