Problem Statement
Most operating systems have some sort of utility to search for a particular file. The order in which these utilites search through subdirectories can make a big difference in how fast a file is found. For example, if a user tends to put files in less nested directories, it would make sense to search those first. You are to write the code for such a utility that sorts a list of directories primarily by directory depth so that directories with lower directory come first.
The algorithm should take a
Definition
- Class:
- Dirsort
- Method:
- sort
- Parameters:
- String[]
- Returns:
- String[]
- Method signature:
- String[] sort(String[] dirs)
- (be sure your method is public)
Notes
- For a directory to exist, it's superdirectory does not necessarily need to exist. For example, you can have "/usr/admin/" without having "/usr/" or even "/".
Constraints
- dirs will contain between 1 and 50 elements, inclusive.
- each element of dirs will be of length 1 to 50, inclusive.
- each element of dirs will contain only lowercase letters [a-z], inclusive, and the slash ('/') character.
- each element of dirs will begin with a slash, end with a slash, and not have double slashes anywhere.
Examples
{"/","/usr/","/usr/local/","/usr/local/bin/","/games/", "/games/snake/","/homework/","/temp/downloads/"}
Returns: { "/", "/games/", "/homework/", "/usr/", "/games/snake/", "/temp/downloads/", "/usr/local/", "/usr/local/bin/" }
Notice that "/temp/downloads/" can exist even without "/temp/".
{"/usr/","/usr/local/","/bin/","/usr/local/bin/", "/usr/bin/","/bin/local/","/bin/local/"}
Returns: { "/bin/", "/usr/", "/bin/local/", "/bin/local/", "/usr/bin/", "/usr/local/", "/usr/local/bin/" }
{"/a/b/c/d/e/f/g/h/i/j/k/l/m/n/","/o/p/q/r/s/t/u/v/w/x/y/z/"}
Returns: { "/o/p/q/r/s/t/u/v/w/x/y/z/", "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/" }
{"/","/a/","/b/","/c/","/d/","/e/","/f/","/g/","/a/a/","/b/g/c/","/g/f/"}
Returns: { "/", "/a/", "/b/", "/c/", "/d/", "/e/", "/f/", "/g/", "/a/a/", "/g/f/", "/b/g/c/" }
{"/a/b/c/d/e/f/g/h/i/j/k/l/m/n/","/o/p/q/r/s/t/u/v/w/x/y/z/"}
Returns: { "/o/p/q/r/s/t/u/v/w/x/y/z/", "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/" }
{"/a/b/","/ab/cd/","/c/d/","/a/b/c/","/ab/c/d/","/a/bc/d/","/a/b/cd/"}
Returns: { "/a/b/", "/ab/cd/", "/c/d/", "/a/b/c/", "/a/b/cd/", "/a/bc/d/", "/ab/c/d/" }
{"/","/usr/","/usr/chogyonim/","/usr/zoidal/","/usr/chogyonim/public/","/usr/chogyonim/private/","/usr/chogyonim/private/code/","/usr/mitalub/","/usr/mitalub/stuff/","/usr/stevevai/","/usr/lbackstrom/","/topcoder/","/topcoder/solutions/","/topcoder/problems/","/topcoder/problems/easy/","/topcoder/problems/medium/","/topcoder/problems/hard/"}
Returns: { "/", "/topcoder/", "/usr/", "/topcoder/problems/", "/topcoder/solutions/", "/usr/chogyonim/", "/usr/lbackstrom/", "/usr/mitalub/", "/usr/stevevai/", "/usr/zoidal/", "/topcoder/problems/easy/", "/topcoder/problems/hard/", "/topcoder/problems/medium/", "/usr/chogyonim/private/", "/usr/chogyonim/public/", "/usr/mitalub/stuff/", "/usr/chogyonim/private/code/" }
{"/z/","/z/y/","/z/y/x/","/z/y/x/w/","/z/y/x/w/v/","/z/y/x/w/v/u/","/z/y/x/w/v/u/t/","/z/y/x/w/v/u/t/s/"}
Returns: { "/z/", "/z/y/", "/z/y/x/", "/z/y/x/w/", "/z/y/x/w/v/", "/z/y/x/w/v/u/", "/z/y/x/w/v/u/t/", "/z/y/x/w/v/u/t/s/" }
{"/","/","/","/","/"}
Returns: { "/", "/", "/", "/", "/" }
{"/","/food/","/food/fruits/","/food/vegetables/","/food/meats/","/food/meats/chicken/","/food/meats/beef/","/food/meats/turkey/","/food/meats/turkey/sandwich/","/food/meats/turkey/sliced/","/food/meats/beef/ribeye/well/","/food/meats/beef/tbone/well/","/food/meats/beef/newyork/well/","/food/meats/beef/ribeye/medium/","/food/meats/beef/tbone/medium/","/food/meats/beef/newyork/medium/","/food/meats/beef/ribeye/rare/","/food/meats/beef/tbone/rare/","/food/meats/beef/newyork/rare/","/drinks/","/drinks/soda/coke/","/drinks/soda/sprite/","/drinks/soda/rootbeer/","/drinks/alcoholic/vodka/","/drinks/alcoholic/gin/","/drinks/alcoholic/rubbing/"}
Returns: { "/", "/drinks/", "/food/", "/food/fruits/", "/food/meats/", "/food/vegetables/", "/drinks/alcoholic/gin/", "/drinks/alcoholic/rubbing/", "/drinks/alcoholic/vodka/", "/drinks/soda/coke/", "/drinks/soda/rootbeer/", "/drinks/soda/sprite/", "/food/meats/beef/", "/food/meats/chicken/", "/food/meats/turkey/", "/food/meats/turkey/sandwich/", "/food/meats/turkey/sliced/", "/food/meats/beef/newyork/medium/", "/food/meats/beef/newyork/rare/", "/food/meats/beef/newyork/well/", "/food/meats/beef/ribeye/medium/", "/food/meats/beef/ribeye/rare/", "/food/meats/beef/ribeye/well/", "/food/meats/beef/tbone/medium/", "/food/meats/beef/tbone/rare/", "/food/meats/beef/tbone/well/" }
{"/a/a/a/a/a/a/","/a/a/a/a/a/b/","/a/a/a/a/a/c/","/a/a/a/a/a/d/","/a/a/a/a/a/e/","/a/a/a/a/a/g/","/a/a/a/a/a/f/","/a/a/a/a/a/i/","/a/a/a/a/a/h/","/a/a/a/a/a/j/","/a/a/a/a/a/l/","/a/a/a/a/a/k/","/a/a/a/a/a/m/","/a/a/a/a/a/n/","/a/a/a/a/a/p/","/a/a/a/a/a/o/","/a/a/a/a/a/q/","/a/a/a/a/a/s/","/a/a/a/a/a/r/","/a/a/a/a/a/t/","/a/a/a/a/a/v/","/a/a/a/a/a/u/","/a/a/a/a/a/z/","/a/a/a/a/a/x/","/a/a/a/a/a/y/","/a/a/a/a/a/w/"}
Returns: { "/a/a/a/a/a/a/", "/a/a/a/a/a/b/", "/a/a/a/a/a/c/", "/a/a/a/a/a/d/", "/a/a/a/a/a/e/", "/a/a/a/a/a/f/", "/a/a/a/a/a/g/", "/a/a/a/a/a/h/", "/a/a/a/a/a/i/", "/a/a/a/a/a/j/", "/a/a/a/a/a/k/", "/a/a/a/a/a/l/", "/a/a/a/a/a/m/", "/a/a/a/a/a/n/", "/a/a/a/a/a/o/", "/a/a/a/a/a/p/", "/a/a/a/a/a/q/", "/a/a/a/a/a/r/", "/a/a/a/a/a/s/", "/a/a/a/a/a/t/", "/a/a/a/a/a/u/", "/a/a/a/a/a/v/", "/a/a/a/a/a/w/", "/a/a/a/a/a/x/", "/a/a/a/a/a/y/", "/a/a/a/a/a/z/" }
{"/emacs/","/emacs/bin/","/emacs/etc/","/emacs/etc/e/","/emacs/etc/icons/","/emacs/info/","/emacs/lisp/","/emacs/lisp/calendar/","/emacs/lisp/emacslisp/","/emacs/lisp/emulation/","/emacs/lisp/eshell/","/emacs/lisp/gnus/","/emacs/lisp/international/","/emacs/lisp/language/","/emacs/lisp/mail/","/emacs/lisp/net/","/emacs/lisp/obsolete/","/emacs/lisp/play/","/emacs/lisp/progmodes/","/emacs/lisp/term/","/emacs/lisp/textmodes/","/emacs/lisp/toolbar/","/emacs/sitelisp/"}
Returns: { "/emacs/", "/emacs/bin/", "/emacs/etc/", "/emacs/info/", "/emacs/lisp/", "/emacs/sitelisp/", "/emacs/etc/e/", "/emacs/etc/icons/", "/emacs/lisp/calendar/", "/emacs/lisp/emacslisp/", "/emacs/lisp/emulation/", "/emacs/lisp/eshell/", "/emacs/lisp/gnus/", "/emacs/lisp/international/", "/emacs/lisp/language/", "/emacs/lisp/mail/", "/emacs/lisp/net/", "/emacs/lisp/obsolete/", "/emacs/lisp/play/", "/emacs/lisp/progmodes/", "/emacs/lisp/term/", "/emacs/lisp/textmodes/", "/emacs/lisp/toolbar/" }
{"/mpthree/","/mpthree/wav/","/mpthree/albums/","/mpthree/albums/matrixsoundtrack/","/mpthree/albums/phantommenace/","/mpthree/albums/bachbrandenburgconcertos/","/mpthree/albums/stanfordfleetstreetfearless/","/mpthree/albums/verticalhorizoneverythingyouwant/","/mpthree/albums/googoodollsdizzyupthegirl/","/mpthree/albums/threedoorsdownthebetterlife/","/mpthree/albums/redhotchilipeppersbytheway/","/mpthree/classical/"}
Returns: { "/mpthree/", "/mpthree/albums/", "/mpthree/classical/", "/mpthree/wav/", "/mpthree/albums/bachbrandenburgconcertos/", "/mpthree/albums/googoodollsdizzyupthegirl/", "/mpthree/albums/matrixsoundtrack/", "/mpthree/albums/phantommenace/", "/mpthree/albums/redhotchilipeppersbytheway/", "/mpthree/albums/stanfordfleetstreetfearless/", "/mpthree/albums/threedoorsdownthebetterlife/", "/mpthree/albums/verticalhorizoneverythingyouwant/" }
{"/","/a/","/c/d/","/a/b/","/c/a/","/c/e/d/","/a/c/d/e/","/c/a/d/","/c/a/e/d/","/a/s/d/","/a/d/s/a/","/a/a/s/d/s/","/a/b/d/s/","/a/w/e/s/","/a/b/e/","/z/","/a/z/g/","/z/g/a/","/z/z/z/z/","/a/g/w/","/s/z/a/"}
Returns: { "/", "/a/", "/z/", "/a/b/", "/c/a/", "/c/d/", "/a/b/e/", "/a/g/w/", "/a/s/d/", "/a/z/g/", "/c/a/d/", "/c/e/d/", "/s/z/a/", "/z/g/a/", "/a/b/d/s/", "/a/c/d/e/", "/a/d/s/a/", "/a/w/e/s/", "/c/a/e/d/", "/z/z/z/z/", "/a/a/s/d/s/" }
{"/","/i/","/i/need/","/i/need/good/","/i/need/good/examples/","/i/want/","/i/want/women/","/i/want/food/"}
Returns: { "/", "/i/", "/i/need/", "/i/want/", "/i/need/good/", "/i/want/food/", "/i/want/women/", "/i/need/good/examples/" }
{ "/", "/a/", "/d/", "/b/" }
Returns: { "/", "/a/", "/b/", "/d/" }
{ "/", "/", "/usr/", "/user/", "/a/", "/a/user/worker/integer/" }
Returns: { "/", "/", "/a/", "/user/", "/usr/", "/a/user/worker/integer/" }
{ "/usr/", "/usr/local/bin/", "/usr/local/" }
Returns: { "/usr/", "/usr/local/", "/usr/local/bin/" }
{ "/a/b/", "/ab/cd/", "/c/d/", "/a/b/c/", "/a/b/cd/", "/a/bc/d/", "/ab/c/d/", "/", "/yoyo/" }
Returns: { "/", "/yoyo/", "/a/b/", "/ab/cd/", "/c/d/", "/a/b/c/", "/a/b/cd/", "/a/bc/d/", "/ab/c/d/" }
{ "/a/", "/b/", "/aa/", "/bb/", "/ab/", "/cc/", "/ac/", "/ca/", "/aaa/" }
Returns: { "/a/", "/aa/", "/aaa/", "/ab/", "/ac/", "/b/", "/bb/", "/ca/", "/cc/" }
{ "/a/", "/a/b/d/f/g/h/j/k/l/o/", "/b/v/" }
Returns: { "/a/", "/b/v/", "/a/b/d/f/g/h/j/k/l/o/" }
{ "/d/e/f/", "/a/a/b/", "/a/a/a/", "/c/c/c/" }
Returns: { "/a/a/a/", "/a/a/b/", "/c/c/c/", "/d/e/f/" }
{ "/a/b/c/", "/de/fg/" }
Returns: { "/de/fg/", "/a/b/c/" }
{ "/ba/", "/b/", "/a/" }
Returns: { "/a/", "/b/", "/ba/" }
{ "/aa/a/", "/aa/a/", "/a/aa/", "/a/aa/", "/aa/a/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/a/aa/", "/aa/a/", "/a/aa/", "/aa/a/", "/a/aa/", "/a/aa/", "/aa/a/", "/a/aa/", "/a/aa/", "/aa/a/", "/a/aa/", "/a/aa/", "/aa/a/", "/aa/a/", "/a/aa/", "/aa/a/", "/a/aa/", "/aa/a/", "/a/aa/", "/aa/a/", "/aa/a/", "/aa/a/", "/a/aa/", "/aa/a/", "/aa/a/", "/a/aa/", "/aa/a/", "/aa/a/", "/a/aa/", "/aa/a/", "/aa/a/", "/aa/a/", "/a/aa/" }
Returns: { "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/a/aa/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/", "/aa/a/" }
{ "/", "/", "/", "/a/" }
Returns: { "/", "/", "/", "/a/" }
{ "/aaaaaaaaaaaa/aaaaaaaaa/aa/", "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" }
Returns: { "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/", "/aaaaaaaaaaaa/aaaaaaaaa/aa/" }
{ "/a/", "/a/b/", "/aa/a/", "/b/f/e/", "/b/f/ea/", "/c/", "/b/", "/aa/", "/b/c/", "/bb/", "/d/v/ff/" }
Returns: { "/a/", "/aa/", "/b/", "/bb/", "/c/", "/a/b/", "/aa/a/", "/b/c/", "/b/f/e/", "/b/f/ea/", "/d/v/ff/" }
{ "/", "/usr/", "/usr/home/", "/games/" }
Returns: { "/", "/games/", "/usr/", "/usr/home/" }
{ "/a/", "/z/", "/ab/", "/aa/" }
Returns: { "/a/", "/aa/", "/ab/", "/z/" }
{ "/c/", "/a/", "/aa/", "/cc/", "/c/a/", "/c/aa/", "/c/a/b/d/", "/cc/aa/bb/", "/c/f/b/", "/c/f/b/a/", "/d/", "/z/" }
Returns: { "/a/", "/aa/", "/c/", "/cc/", "/d/", "/z/", "/c/a/", "/c/aa/", "/c/f/b/", "/cc/aa/bb/", "/c/a/b/d/", "/c/f/b/a/" }
{ "/usr/", "/usr/local/", "/bin/", "/usr/local/bin/", "/usr/bin/", "/bin/local/", "/bin/local/" }
Returns: { "/bin/", "/usr/", "/bin/local/", "/bin/local/", "/usr/bin/", "/usr/local/", "/usr/local/bin/" }
{ "/e/d/e/ds/w/s/s/s/a/", "/as/ds/sd/ds/xc/fr/rt/df/sd/ds/sd/", "/dfss/ds/sd/vc/er/thg/sdv/as/dfa/asd/fd/sdf/", "/", "/e/e/wew/ewe/wew/", "/df/dsdf/vcxv/eerd/ewds/ewwed/ewedf/wwes/" }
Returns: { "/", "/e/e/wew/ewe/wew/", "/df/dsdf/vcxv/eerd/ewds/ewwed/ewedf/wwes/", "/e/d/e/ds/w/s/s/s/a/", "/as/ds/sd/ds/xc/fr/rt/df/sd/ds/sd/", "/dfss/ds/sd/vc/er/thg/sdv/as/dfa/asd/fd/sdf/" }
{ "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/" }
Returns: { "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/", "/a/" }