KatInterview

Longest Repeat-Free Concatenation, Lexicographically Smallest

StringsAuto-graded · 22 test cases
MediumAuto-gradedStringsbacktrackingsubsetsstrings

Last updated

Pick words and order them to form the longest string with no repeated character, breaking ties by smallest lexicographic order; tests subset backtracking with disjoint-set pruning.

Longest Repeat-Free Concatenation

You are given a list of strings words. Choose any subset of the list entries (possibly none), put the chosen strings in any order, and join them end to end into a single string.

A joined string is valid if no character appears more than once in it. Characters are compared exactly (case-sensitive, so "a" and "A" are different characters).

What to return

Consider every valid string obtainable by any choice of subset and any ordering of it. Return the one that is best under these rules, applied in order:

  1. It has the greatest length.
  2. Among strings of that greatest length, it is the lexicographically smallest. Strings are compared character by character using character code points (Python's default < on str).

If the only valid string is the empty string (for example, every word repeats a character), return "".

Precise rules

  • Each list entry may be used at most once. If the same word appears twice in the list, those are two separate entries, but two copies of a non-empty word can never both be chosen, since their characters would repeat.
  • A word that contains a repeated character by itself can never be chosen.
  • An empty word "" is a legal entry. Choosing it changes nothing in the resulting string.
  • Only the final string is returned. The same string may arise from different subsets or orders, which is fine.
  • The tie-break is over the final strings, across all subsets and all orderings. It is not just a tie-break between subsets. For a fixed subset, you must use the ordering that gives the smallest string, and then compare these across subsets.
  • Different subsets with the same total length can yield different strings, for example ["un", "iq", "ue"] can give "iqun" or "ique". Return the smaller one.
  • The answer is a string of distinct characters, so its length never exceeds the number of distinct characters in the input.

Input

  • words: a list of strings.

Output

  • A string: the longest valid concatenation, with ties broken by smallest lexicographic order, or "" if none is non-empty.

Constraints

  • 1 <= len(words) <= 16
  • Words may be empty. Words may contain repeated characters.
  • Characters are compared by code point. Do not assume the input is only lowercase letters, and do not hard-code a 26-letter alphabet.
  • Total length of all words is small (at most a few hundred characters).

Examples

Example 1

Input: longest_unique_concat_smallest(['un', 'iq', 'ue'])

Output: 'ique'

Example 2

Input: longest_unique_concat_smallest(['aa', 'bc', 'd'])

Output: 'bcd'

Example 3

Input: longest_unique_concat_smallest(['ab', 'cd', 'ef'])

Output: 'abcdef'

Example 4

Input: longest_unique_concat_smallest(['a', 'b', 'c', 'ab'])

Output: 'abc'

Hints

Hint 1

Discard any word that repeats a character internally. For the rest, only the set of characters in each word matters for checking compatibility; do not assume lowercase only.

Hint 2

With at most 16 words there are at most 65,536 subsets. Recurse over indices in increasing order, adding a word only if it shares no character with the characters already used.

Hint 3

For a fixed subset, the smallest string comes from sorting its words so that a goes before b when a+b < b+a. Then compare each subset's result with the best so far by length first, and by lexicographic order if the lengths are equal.