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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code