why there are test cases containing " " instead of '\t' ? How to handle it?


  • 1
    F

    Below is my code, and it failed at test case "dir\n file.txt". So the input has four blank space characters (' ') instead of one '\t'. Apparently my code won't be able to handle it. But when I see the solution from yours, none of you have special handle on blank space ' '. So I am confused. Please help

    public:
        int lengthLongestPath(string input) {
            stack<string> s;
            int i = 0;
            int ret = 0;
            int total = 0;
            while (i < input.size()) {
                
                int depth = 0;
                
                while (i < input.size() && input[i] == '\t') {
                    i++;
                    depth++;
                }
                
                int start = i;
                bool is_file = false;
                while (i < input.size() && input[i] != '\n') {
                    if (input[i] == '.') {
                        is_file = true;
                    }
                    i++;
                }
                if (is_file) {
                    ret = max(ret, total + i - start + depth);
                } else {
                    while (s.size() > depth) {
                        string top = s.top();
                        s.pop();
                        total -= top.size();
                    }
                    s.push(input.substr(start, i - start));
                    total += i - start;
                }
                i++;
            }
            
            return ret;
        }
    };
    
    
    

Log in to reply
 

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