clueless coding // TODO: be smarter

LeetCode Longest Absolute File Path: CPP Solution



TL;DR Code Solution


int lengthLongestPath(string input) {
    stack<int> stac;
    int level = 0;
    int current = 0;
    int count = 0;
    int longest = 0;
    bool file = false;
    for (int i = 0; i < input.length(); i++) {

        if (input[i] == '\n') {
            continue;
        }

        level = 0;
        while (input[i] == '\t' && i < input.length()) {
            level++;
            i++;
        }

        while (level < stac.size()) {
            current -= stac.top();
            stac.pop();
        }

        file = false;
        count = 0;
        while (input[i] != '\n'  && i < input.length()) {
            if (input[i] == '.') {
                file = true;
            }
            count++;
            i++;
        }

        current += count + 1;

        if (file) {
            longest = max(longest, current - 1);
        }

        stac.push(count + 1);

    }

    return longest;

}