title: Check if given string matches one of set of prefixes, effectively
body: <p>What algorithm to use to check if a given string matches one of set of prefixes, and which prefix from that set?</p>
  
  <p>Other variation: given path and a set of directories, how to check if path is in one of set of directories (assuming that there are no symbolic links, or they do not matter)?</p>
  
  <p>I'm interested in description or name of algorithm, or Perl module which solves this (or can be used to solve this).</p>
  
  <p><strong>Edit</strong><br>
  Bonus points for solution which allow to effectively find <em>'is prefix of'</em> relation between set of strings (set of directories)</p>
  
  <p>For example, given set of directories: <code>foo, foo/bar, foo/baz, quux, baz/quux, baz/quux/plugh</code> the algorithm is to find that <code>foo</code> is prefix of <code>foo/bar</code> and <code>foo/baz</code>, and that <code>baz/quux</code> is prefix of <code>baz/quux/plugh</code>... hopefully without O(n^2) time.</p>