This is so far the worst problem I've seen in Leetcode !


  • 0
    H

    @ManuelP

    Under this question,
    Can someone please explain what does simplify means in this context?

    You @ManuelP said:

    If you're asking such questions, maybe IT isn't for you...

    For me, the definition of "Simplify" is not clear.

    [1] Should we really remove the slash forward following the final item?
    Do you think it's just useless? Unfortunately the final slash indicates that final item is a directory rather than a file. Do you dare to remove final slash from your colleague‘s code?

    [2] should I simplify "../a" to "/a"?
    "../a" look up for “a" in parent directory, "/a" is just under the ROOT directory. Totally two different things.

    [3] Can we add a slash to each element? "a/" becomes "/a"?
    "a/" is "a" directory under current directory. "/a" is "a" under ROOT directory. Not the same.

    This is the 1st voted solution so far (123 vote up).

    /**
     *  Skip all ".."? 
     *  For test case: "../a", the output is "/a"?
     *  Add a "/" to each element?
     *  Test case: "a/",    Output: "/a". 
     */
    string simplifyPath(string path) {
        string res, tmp;
        vector<string> stk;
        stringstream ss(path);
        while(getline(ss,tmp,'/')) {
            if (tmp == "" or tmp == ".") continue;
            if (tmp == ".." and !stk.empty()) stk.pop_back();
            else if (tmp != "..") stk.push_back(tmp);  // BOOM! Skip all .. ?
        }
        for(auto str : stk) res += "/"+str; // BOOM! Add "/" to each element?
        return res.empty() ? "/" : res;
    }
    

    The following is 2nd voted solution, 89 vote up,

       /**
        *  Skip all ".."? 
        *  For test case: "../a", the output is "/a"?
        *  Add a "/" to each element?
        *  Test case: "a/",    Output: "/a".   Kidding me?
        */
        public String simplifyPath(String path) {
            Deque<String> stack = new LinkedList<>();
            Set<String> skip = new HashSet<>(Arrays.asList("..",".",""));
            for (String dir : path.split("/")) {
                if (dir.equals("..") && !stack.isEmpty()) {
                    stack.pop();
                } else if (!skip.contains(dir)) {  // BOOM! Skip all .. ? Kidding me ?
                    stack.push(dir); 
                }
            }
            String res = "";
            for (String dir : stack) res = "/" + dir + res; // BOOM! Add "/" to each element?
            return res.isEmpty() ? "/" : res;
        }
    

    If most of the top voted AC solutions are full of bugs, do you think this is a good leetcode problem?


  • 0

    Edit: The below is my reply to the original message, not to the very different one it was changed to after I had posted. Originally I also wasn't addressed (like helloShen now pretends).

    It's just the usual simplification. What makes you think that "/..." is not a valid expression? It simply refers to subdirectory "..." inside the root directory, no? What does that have to do with parent directories? As far as I know, there's nothing special about "...". You didn't even get an "invalid expression" error or so, you were just told that that directory doesn't exist. What do you get for it after you've done mkdir /...?


  • 0
    H
    This post is deleted!

  • 0

    You should really not lie about other people's solutions. The comments in them were added by you.

    The problem clearly states "Given an absolute path" and even the forward slash page you yourself had linked to before you hid it, clearly states "every absolute path [...] begins with a forward slash". So I have no idea why you're now talking about inputs "../a" and "a/", as they're obviously invalid, as shown by your own reference. And no, your invalid inputs don't show bugs in solutions, they just show bugs in your thinking.


  • 0
    H

    @ManuelP

    My fault! I didn't see the word absolute in the question.

    Two suggestions:

    1. highlight the "absolute" in the question.
    2. add some formal explanation about "simplify"

    Last thing, @ManuelP , we are just talking about the code. Code is code, but nothing about "personality". Do not always judge people:

    You should really not lie about other people's solutions.

    If you're asking such questions, maybe IT isn't for you...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.