C# implementation - not using stacks - passing all cases, no need to treat \t specially.


  • 0
    R

    '''
    public class Solution
    {
    public int LengthLongestPath(string input)
    {
    var chuncks = input.Split('\n');
    var maxSoFar = chuncks[0].IndexOf('.') > 0 ? chuncks[0].Length : 0;
    var path = new List<string>();
    path.Add(chuncks[0]);
    for (int i = 1; i < chuncks.Length; ++i)
    {
    var chunck = chuncks[i];
    int localLevel = chunck.LastIndexOf('\t')+1;
    chunck = chuncks[i].Substring(localLevel);
    if (localLevel < path.Count)
    {
    path.RemoveRange(localLevel, path.Count - localLevel);
    }
    path.Add(chunck);
    int isFile = chunck.IndexOf('.');
    if (isFile > 0)
    {
    var pathString = string.Join("/", path);
    maxSoFar = Math.Max(maxSoFar, pathString.Length);
    }
    }
    return maxSoFar;
    }
    }
    '''


Log in to reply
 

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