Statistics

Problem Statement for "Dirsort"

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 String[] dirs as an input and should sort dirs first by directory depth, and then lexicographically for each depth. So "/d/e/" comes before "/a/b/c/", but not before "/c/d/". Also, "/a/bc/", comes before "/ab/c/", since "a" comes before "ab" in lexicographical order.

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

  1. {"/","/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/".

  2. {"/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/" }

  3. {"/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/" }

  4. {"/","/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/" }

  5. {"/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/" }

  6. {"/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/" }

  7. {"/","/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/" }

  8. {"/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/" }

  9. {"/","/","/","/","/"}

    Returns: { "/", "/", "/", "/", "/" }

  10. {"/","/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/" }

  11. {"/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/" }

  12. {"/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/" }

  13. {"/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/" }

  14. {"/","/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/" }

  15. {"/","/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/" }

  16. { "/", "/a/", "/d/", "/b/" }

    Returns: { "/", "/a/", "/b/", "/d/" }

  17. { "/", "/", "/usr/", "/user/", "/a/", "/a/user/worker/integer/" }

    Returns: { "/", "/", "/a/", "/user/", "/usr/", "/a/user/worker/integer/" }

  18. { "/usr/", "/usr/local/bin/", "/usr/local/" }

    Returns: { "/usr/", "/usr/local/", "/usr/local/bin/" }

  19. { "/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/" }

  20. { "/a/", "/b/", "/aa/", "/bb/", "/ab/", "/cc/", "/ac/", "/ca/", "/aaa/" }

    Returns: { "/a/", "/aa/", "/aaa/", "/ab/", "/ac/", "/b/", "/bb/", "/ca/", "/cc/" }

  21. { "/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/" }

  22. { "/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/" }

  23. { "/a/b/c/", "/de/fg/" }

    Returns: { "/de/fg/", "/a/b/c/" }

  24. { "/ba/", "/b/", "/a/" }

    Returns: { "/a/", "/b/", "/ba/" }

  25. { "/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/" }

  26. { "/", "/", "/", "/a/" }

    Returns: { "/", "/", "/", "/a/" }

  27. { "/aaaaaaaaaaaa/aaaaaaaaa/aa/", "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" }

    Returns: { "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/", "/aaaaaaaaaaaa/aaaaaaaaa/aa/" }

  28. { "/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/" }

  29. { "/", "/usr/", "/usr/home/", "/games/" }

    Returns: { "/", "/games/", "/usr/", "/usr/home/" }

  30. { "/a/", "/z/", "/ab/", "/aa/" }

    Returns: { "/a/", "/aa/", "/ab/", "/z/" }

  31. { "/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/" }

  32. { "/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/" }

  33. { "/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/" }

  34. { "/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/" }


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: