Find the longest prefix of a string for a set of characters