Length of file path to image file


  • 0
    S

    Input: "dir\n dir2\n dir3\n \image.jpeg"
    Output: 11

    Find the sum of file path lengths leading to image files (.jpeg, .jpg, .png).

    The spaces represent the depth in the file path. There could be file paths leading to a .txt file but you don't care about those.

    Should be solved in O(n) time.


  • 0
    D

    Actually, every directory structure is a tree (assuming Unix). They are asking sum of all depths of leaf nodes that ends with .jpeg, .jpg and .png. DFS is an ideal solution. In window, you have multiple roots unlike Unix where it has only one root.


  • 0
    D

    BFS also gives the solution to the problem. Create a queue and you can do it in O(n) with the space complexity of O(n).


Log in to reply
 

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