TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

DirectoryMapping

Problem statement, definition, constraints, and public examples.

Problem Statement

When your webserver gets file requests it first maps the requested location to an actual location on the drive. The internal mapping cache will be given in a String[] called mappings. Each element of mappings will have the form (quotes for clarity) "pathL -> pathR". A path will be a list of names delimited by '/' characters. In addition, the first and last characters will be '/' characters. For example, "/base/exec/two/ -> /usr/bin/two/" would be a well formatted element of mappings. You will also be given a String input representing the path from the file request. It will be formatted like the paths in mappings. Your code will search mappings for the longest pathL that is a prefix of input (case-sensitive). The pathL can match the entire input. You will then replace that prefix with the corresponding pathR and return the resulting path. If two elements both match, use the one that occurs earlier in mappings. If none match, simply return the input unscathed.

Definition

Class:
DirectoryMapping
Method:
getMapping
Parameters:
String[], String
Returns:
String
Method signature:
String getMapping(String[] mappings, String input)
(be sure your method is public)

Constraints

  • mappings will contain between 0 and 50 elements inclusive.
  • Each element of mappings will contain between 3 and 50 characters inclusive.
  • input will contain between 3 and 50 characters inclusive.
  • input will match the following Java regular expression (quotes for clarity): "/([A-Za-z]+/)+".
  • Each element of mappings will have the form (quotes for clarity) "pathL -> pathR" where pathL and pathR are both formatted like input.

Examples

  1. {"/base/exec/two/ -> /usr/bin/two/"}
    "/base/exec/two/great/whatever/"
    Returns: "/usr/bin/two/great/whatever/"
    The mapping discussed earlier.
  2. {"/base/ -> /etc/etc/etc/etc/etc/etc/etc/",
     "/base/exec/two/ -> /usr/bin/two/"}
    "/base/exec/two/great/whatever/"
    Returns: "/usr/bin/two/great/whatever/"
    Both apply so use the one with the longer pathL.
  3. {"/one/two/three/four/ -> /a/"}
    "/one/two/three/four/"
    Returns: "/a/"
    The whole path can be matched.
  4. {"/Abb/ -> /grr/"}
    "/abb/hmm/"
    Returns: "/abb/hmm/"
    Remember that the match is case-sensitive.
  5. {"/one/Two/three/four/ -> /a/",
     "/one/Two/three/four/ -> /bcd/"}
    "/one/Two/three/four/five/"
    Returns: "/a/five/"
    Both match and have the same pathL length so choose the earlier one.
  6. {"/one/two/three/four/ -> /a/",
     "/one/two/three/four/ -> /a/"}
    "/duplicates/ehh/"
    Returns: "/duplicates/ehh/"
    mappings may contain duplicate entries.
  7. {}
    "/dont/map/me/"
    Returns: "/dont/map/me/"
    mappings may contain no entries at all.
Back to all problems