C++ solution using map, 0ms


  • 0
    F
    int lengthLongestPath(string input) {
            int max_count=0;
            int count=0;
            int level=1;
            bool isFile=false;      
            map<int,int> mp;
            for(int i=0;i<input.size();i++){            
                while(i<input.size() && input[i]=='\t'){
                    level++;
                    i++;
                }
                while( i<input.size() && input[i]!='\n'){
                    if(input[i]=='.') isFile=true;              
                    count++; 
                    i++;
                }
                if(isFile)
                    max_count= max(max_count, mp[level-1]+count);
                else     
                    mp[level]= mp[level-1]+count+1;            
                count=0;
                level=1;
                isFile=false;
            }
            
            return max_count;
    }
    

Log in to reply
 

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