Maximum Length of a Concatenated String with Unique Characters - Problem

You are given an array of strings arr. Your task is to find the maximum possible length of a string formed by concatenating a subsequence of these strings, where the resulting string contains only unique characters.

A subsequence is derived from the original array by selecting some (or none) of the strings while maintaining their relative order. For example, from ["a", "b", "c"], valid subsequences include ["a", "c"], ["b"], or [].

Key constraints:

  • The concatenated result must have all unique characters
  • You want to maximize the length of this result
  • Individual strings in the input may already contain duplicate characters (these should be ignored)

Example: Given ["un", "iq", "ue"], you can form "unique" by concatenating all three strings, resulting in length 6.

Input & Output

example_1.py โ€” Basic Case
$ Input: ["un", "iq", "ue"]
โ€บ Output: 6
๐Ÿ’ก Note: We can concatenate all three strings to form "unique" which has 6 unique characters. This is the maximum possible length.
example_2.py โ€” With Invalid Strings
$ Input: ["cha", "r", "act", "ers"]
โ€บ Output: 6
๐Ÿ’ก Note: The best combination is "chaers" (using "cha", "r", and "ers") which has 6 unique characters. We can't include "act" because it would create duplicates with "cha".
example_3.py โ€” Single Character Strings
$ Input: ["a", "b", "c", "d", "e", "f"]
โ€บ Output: 6
๐Ÿ’ก Note: We can concatenate all strings to get "abcdef" with 6 unique characters.

Visualization

Tap to expand
Building Unique Character CombinationsStep 1: Input Array["un", "iq", "ue"] โ†’ All strings have unique internal characters โœ“Step 2: Backtracking Exploration"""un""uniq""unique"Each step checks: Can I add this string without creating duplicates?Step 3: Conflict DetectionCurrent: "uniq" {u,n,i,q}Candidate: "ue" {u,e}Conflict: 'u' โŒโŒ Skip this combination - 'u' appears in both stringsStep 4: Valid Path Found"unique" = "un" + "iq" + "ue"Length = 6 โœ“All characters {u,n,i,q,u,e} โ†’ {u,n,i,q,e} are unique!
Understanding the Visualization
1
Filter Invalid Candidates
Remove any strings that already have duplicate characters - these can never contribute to a valid solution
2
Use Smart Exploration
Try adding each string to your current combination, but check for conflicts first
3
Early Conflict Detection
If adding a string would create duplicates, skip it immediately and try the next option
4
Track Best Solution
Keep updating your best result as you find longer valid combinations
Key Takeaway
๐ŸŽฏ Key Insight: Backtracking with early conflict detection allows us to efficiently explore only valid combinations, avoiding the exponential overhead of checking invalid paths. Using bit manipulation for character sets makes conflict detection O(1).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n * m)

2^n subsequences to check, each taking O(m) time to validate where m is average string length

n
2n
โš  Quadratic Growth
Space Complexity
O(m)

Space for concatenated strings and character frequency checking

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 16
  • 1 โ‰ค arr[i].length โ‰ค 26
  • arr[i] contains only lowercase English letters
  • The maximum possible length will not exceed 26 (since there are only 26 unique lowercase letters)
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
52.3K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen