C# solution


  • 0
    Q

    C# solution

    public class Solution {
        public int LengthLongestPath(string input) {
            var lines = input.Split('\n');
            var max = 0;
            var pathLengthMap = new Dictionary<int, int>();
            pathLengthMap.Add(0, 0);
            for(int i = 0; i< lines.Length; i++){
                var line = lines[i];
                var name = line.TrimStart('\t');
                var depth = line.Length - name.Length+1;
                var length = pathLengthMap[depth - 1];
                length += name.Length;
                // if is file
                if(name.Contains('.')){
                    max = Math.Max(max, length);
                }
                else // if is not file
                {
                    length++;        
                    if(pathLengthMap.ContainsKey(depth)) pathLengthMap[depth] = length;
                    else pathLengthMap.Add(depth, length);
                }
                
            }
            return max;
        }
    }
    

Log in to reply
 

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