TopcoderArchiveVisit Topcoder
SRM11 May 2006
SRM 302

JoinedString

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a String[] words. Return the shortest String that contains all the words as substrings. If there are several possible answers, return the one that comes first lexicographically.

Definition

Class:
JoinedString
Method:
joinWords
Parameters:
String[]
Returns:
String
Method signature:
String joinWords(String[] words)
(be sure your method is public)

Constraints

  • words will contain between 1 and 12 elements, inclusive.
  • Each element of words will contain between 1 and 50 characters, inclusive.
  • Each element of words will consist of only uppercase letters ('A'-'Z').

Examples

  1. {"BAB", "ABA"}
    Returns: "ABAB"
    There are two strings of length 4 that contain both given words: "ABAB" and "BABA". "ABAB" comes earlier lexicographically.
  2. {"ABABA", "AKAKA", "AKABAS", "ABAKA"}
    Returns: "ABABAKAKABAS"
  3. {"AAA","BBB", "CCC", "ABC", "BCA", "CAB"}
    Returns: "AAABBBCABCCC"
  4. {"OFG", "SDOFGJTILM", "KBWNF", "YAAPO", "AWX", "VSEAWX", "DOFGJTIL", "YAA"}
    Returns: "KBWNFSDOFGJTILMVSEAWXYAAPO"
  5. {"NVCSKFLNVS", "HUFSPMRI", "FLNV", "KMQD", "RPJK", "NVSQORP", "UFSPMR", "AIHUFSPMRI"}
    Returns: "AIHUFSPMRINVCSKFLNVSQORPJKMQD"
  6. {"STRING", "RING"}
    Returns: "STRING"
Back to all problems