TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

PrefixChoice

Problem statement, definition, constraints, and public examples.

Problem Statement

Given a String text return the longest prefix which does not have any element of remove as a substring. A substring is a contiguous portion of a string, which may be the entire string. A prefix is a substring taken from the beginning of the string. For example, "wow" is a substring of "awowb", but not a prefix. "awo" is both a substring and prefix of "awowb".

Definition

Class:
PrefixChoice
Method:
longestPrefix
Parameters:
String, String[]
Returns:
String
Method signature:
String longestPrefix(String text, String[] remove)
(be sure your method is public)

Constraints

  • remove will contain between 1 and 50 elements inclusive.
  • Each element of remove will contain between 2 and 50 characters inclusive.
  • Each character in each element of remove will be a lowercase letter ('a'-'z').
  • text will contain between 1 and 50 characters inclusive.
  • Each character in text will be a lowercase letter ('a'-'z').

Examples

  1. "awowb"
    {"wow"}
    Returns: "awo"
    The longest prefix is "awo" since anything longer will contain "wow".
  2. "awowb"
    {"awowb"}
    Returns: "awow"
  3. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
    {"abce","abcdefh","abcdefghijklmnopr"}
    Returns: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
  4. "zzzzzzzzzz"
    {"zz","zzz","zzzz"}
    Returns: "z"
  5. "topcodertopcodertopcodertopcoder"
    {"rtopcodertop"}
    Returns: "topcodertopcoderto"
Back to all problems