There seems to be a wrong test case in the code submission. The test case- ""dir\n file.txt"" should give 12 but the expected output is given 16. Can someone clarify this?


  • 0
    M

    public class Solution {

    boolean isFileName(String input)
    {
        boolean result = false;
        if(input.length() < 4)
            return result;
        if('.' == input.charAt(input.length()-4))
            result = true;
        if('.' == input.charAt(input.length()-3) && input.endsWith("gz"))
            result = true;
        return result;
    }
    
    public int lengthLongestPath(String input) {     
        
        String [] fileNames = input.split("\\n");
        StringBuilder [] longestFiles = new StringBuilder[fileNames.length];
        int longestLength = 0;
        Node root = null ;
        
        Label: for (String s : fileNames)
        {
            int count = s.lastIndexOf('\t');
            String name = s.substring(count+1);
            if (!isFileName(name))
            {
                Node dir = new Node(name);
                if( count == -1 )
                {
                  root = dir;
                  root.length = root.name.length();
                  continue;                
                }
              int length = root.traverse(count, dir, root);
            }
            else
            {
                name = name.trim();
                 Node file = new Node(name);
              int length = 0;
              if(root == null)
              {
                length = file.length = name.length();
    
              }
              else
              {
                 length = root.traverse(count, file, root);
              }
                if(longestLength < length)
                  
                {
                    longestLength = length;
                }
            }
        }
        return longestLength;  
    }
    

    public static void main(String []args)
    {
    int length = new Solution().lengthLongestPath("dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext");
    }
    }

    class Node
    {
    String name;
    int length;
    ArrayList<Node> child = new ArrayList<Node>();

    public Node()
    {
        
    }
    public Node(String name)
    {
        this.name = name;
    }
    public void addChild(Node childNode)
    {
        this.child.add(childNode);
    }
    
    public int traverse(int index, Node node, Node root)
    {
        Node current = root;
        int prevLength = root.length;
        while(index > 0)
        {
          if(current.child.size() > 0) {
            current = current.child.get((current.child.size())-1);
            prevLength = current.length;
          }
          else
          {
            prevLength = prevLength + 1;
          }      
            index--;
        }       
        node.length = prevLength + 1 + node.name.length();        
        current.addChild(node);
        return node.length;
    }
    

    }


Log in to reply
 

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