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
{"BAB", "ABA"}Returns: "ABAB"
There are two strings of length 4 that contain both given words: "ABAB" and "BABA". "ABAB" comes earlier lexicographically.{"ABABA", "AKAKA", "AKABAS", "ABAKA"}Returns: "ABABAKAKABAS"
{"AAA","BBB", "CCC", "ABC", "BCA", "CAB"}Returns: "AAABBBCABCCC"
{"OFG", "SDOFGJTILM", "KBWNF", "YAAPO", "AWX", "VSEAWX", "DOFGJTIL", "YAA"}Returns: "KBWNFSDOFGJTILMVSEAWXYAAPO"
{"NVCSKFLNVS", "HUFSPMRI", "FLNV", "KMQD", "RPJK", "NVSQORP", "UFSPMR", "AIHUFSPMRI"}Returns: "AIHUFSPMRINVCSKFLNVSQORPJKMQD"
{"STRING", "RING"}Returns: "STRING"