Watch 10 video solutions for Maximum Length of a Concatenated String with Unique Characters, a medium level problem involving Array, String, Backtracking. This walkthrough by NeetCode has 33,479 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All the valid concatenations are:
- ""
- "un"
- "iq"
- "ue"
- "uniq" ("un" + "iq")
- "ique" ("iq" + "ue")
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"] Output: 26 Explanation: The only string in arr has all 26 characters.
Constraints:
1 <= arr.length <= 161 <= arr[i].length <= 26arr[i] contains only lowercase English letters.Problem Overview: You receive an array of strings and need to select any subset whose concatenation contains only unique characters. The goal is to maximize the length of the resulting string. If two chosen strings share a character, that combination becomes invalid.
Approach 1: Recursive Backtracking with Character Set (Time: O(2^n * L), Space: O(n + L))
This approach explores all possible subsets using backtracking. Start with an empty string and recursively decide whether to include each word. Before adding a word, check whether its characters conflict with the current combination using a set or frequency array. If no overlap exists, append the string and continue exploring deeper combinations. The recursion naturally generates all valid subsets, while pruning occurs whenever duplicate characters appear.
Character validation requires iterating through the candidate string and checking membership in a set. If the string itself contains duplicate characters, skip it early. The search tree contains at most 2^n branches, where n is the number of strings. This approach is straightforward and mirrors the subset generation pattern frequently seen in interview problems involving array exploration and string validation.
Approach 2: Bitmasking for Character Uniqueness Check (Time: O(2^n), Space: O(n))
This approach compresses each string into a 26-bit integer mask where each bit represents a lowercase character. If a string contains duplicate characters internally, discard it immediately because its mask would collide with itself. When combining strings, use a bitwise AND operation to check for overlapping characters: maskA & maskB. If the result is zero, the strings share no characters and can be merged using bitwise OR.
Backtracking or iterative subset building works efficiently with these masks. Instead of scanning characters repeatedly, each uniqueness check becomes a constant-time bit operation. This significantly reduces overhead compared to set comparisons. The algorithm maintains a list of valid masks and builds larger combinations while tracking the maximum bit count (string length). Bit manipulation drastically improves constant factors while preserving the same exponential subset exploration.
Recommended for interviews: Start by explaining the recursive backtracking idea because it demonstrates understanding of subset generation and constraint pruning. Then optimize using bit manipulation to represent characters as masks. Interviewers usually expect the bitmask approach because it replaces repeated character scans with constant-time operations and shows deeper familiarity with low-level optimization.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Backtracking with Character Set | O(2^n * L) | O(n + L) | When implementing quickly in interviews or explaining subset exploration logic |
| Bitmasking for Character Uniqueness | O(2^n) | O(n) | When optimizing character checks using bit operations for faster validation |