Problem · Array

Maximum Unique-Character Word Subset

Learn this problem
MediumMeta logoMetaFULLTIMEONSITE INTERVIEW
See Meta hiring insights

Problem statement

Given an array of lowercase English words, choose a subset whose concatenation contains no repeated character. Return the maximum possible length of such a concatenation.

A word that repeats a character inside itself cannot be selected. The order of selected words does not affect validity, and the empty subset is allowed. Return only the maximum length, not the selected subset.

Function

maxUniqueCharacterSubsetLength(words: String[]) → int

Examples

Example 1

words = ["un","iq","ue"]return = 4

Selecting un and iq produces four distinct characters. Adding ue would repeat u.

Example 2

words = ["cha","r","act","ers"]return = 6

Selecting cha and ers produces the six distinct characters in chaers.

Example 3

words = ["aa","bc"]return = 2

The word aa is invalid because it repeats a internally. Selecting only bc gives length 2.

Constraints

  • 0 <= words.length <= 16.
  • 1 <= words[i].length <= 26.
  • Every word contains only lowercase English letters.

More Meta problems

drafts saved locally
public int maxUniqueCharacterSubsetLength(String[] words) {
    // Write your code here.
}
words["un","iq","ue"]
expected4
checking account