Maximum Length of a Concatenated String with Unique Characters - Problem

You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.

Return the maximum possible length of s.

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

Input & Output

Example 1 — Basic Concatenation
$ Input: arr = ["un","iq","ue"]
Output: 4
💡 Note: We can concatenate "un" + "iq" = "uniq" which has 4 unique characters. Other combinations like "unue" have duplicate 'u', so length 4 is maximum.
Example 2 — All Single Characters
$ Input: arr = ["cha","r","act","ers"]
Output: 6
💡 Note: We need to find the longest concatenation with all unique characters. Possible combinations include: "cha" (3), "r" (1), "act" (3), "ers" (3), "cha"+"r"="char" (4), "r"+"act"="ract" (4), and "act"+"ers"="acters" (6 unique characters). The maximum length is 6.
Example 3 — No Valid Combination
$ Input: arr = ["aa","bb"]
Output: 0
💡 Note: Each string itself contains duplicate characters ("aa" has two 'a's, "bb" has two 'b's), so we cannot use any string. Maximum length is 0.

Constraints

  • 1 ≤ arr.length ≤ 16
  • 1 ≤ arr[i].length ≤ 26
  • arr[i] contains only lowercase English letters

Visualization

Tap to expand
Maximum Length Concatenated String INPUT arr = ["un", "iq", "ue"] "un" index 0 "iq" index 1 "ue" index 2 Character Sets: "un" : {u, n} "iq" : {i, q} "ue" : {u, e} Find max length with unique characters only ALGORITHM STEPS 1 Filter Valid Strings Skip strings with duplicate chars 2 Use Bitmask Represent chars as bit positions (a=0,b=1..) 3 Backtrack / DFS Try include/exclude each string 4 Check Overlap mask1 AND mask2 == 0 means no conflict Valid Combinations: "un"+"iq" = "uniq" (4) "iq"+"ue" = "ique" (4) "un"+"ue" = conflict (u) "un"+"iq"+"ue" = conflict FINAL RESULT Best Concatenation: "uniq" "un" + "iq" u n i q All characters unique! Output: 4 Maximum length achieved: 4 chars OK Key Insight: Use bitmasks to track character usage efficiently. Each character maps to a bit (a=bit 0, b=bit 1, etc). Two strings can be concatenated if (mask1 AND mask2) == 0, meaning no overlapping characters. Backtracking explores all valid subsequences, keeping track of the maximum length found. TutorialsPoint - Maximum Length of a Concatenated String with Unique Characters | Optimal Solution
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
78.0K Views
Medium Frequency
~25 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