Skip to main content

Maximum Length of a Concatenated String with Unique Characters

LeetCode 1360 | Difficulty: Medium​

Medium

Problem Description​

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 <= 16`

- `1 <= arr[i].length <= 26`

- `arr[i]` contains only lowercase English letters.

Topics: Array, String, Backtracking, Bit Manipulation


Approach​

Backtracking​

Explore all candidates by building solutions incrementally. At each step, choose an option, explore further, then unchoose (backtrack) to try the next option. Prune branches that can't lead to valid solutions.

When to use

Generate all combinations/permutations, or find solutions that satisfy constraints.

Bit Manipulation​

Operate directly on binary representations. Key operations: AND (&), OR (|), XOR (^), NOT (~), shifts (<<, >>). XOR is especially useful: a ^ a = 0, a ^ 0 = a.

When to use

Finding unique elements, power of 2 checks, subset generation, toggling flags.

String Processing​

Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.

When to use

Anagram detection, palindrome checking, string transformation, pattern matching.


Solutions​

Solution 1: C# (Best: 160 ms)​

MetricValue
Runtime160 ms
Memory27.3 MB
Date2020-03-15
Solution
public class Solution {
public int MaxLength(IList<string> arr) {
int result = 0;
DfsMaxLength(arr, "", 0, ref result);
return result;
}

private void DfsMaxLength(IList<string> arr, string path, int index, ref int result)
{
bool isUnique = IsUnique(path);
if (isUnique)
{
result = Math.Max(result, path.Length);
}

if (index == arr.Count || !isUnique) return;
for (int i = index; i < arr.Count; i++)
{
DfsMaxLength(arr, path+arr[i], i+1, ref result);
}
}


private bool IsUnique(string str)
{
HashSet<char> list = new HashSet<char>(str);
return list.Count == str.Length;
}
}

Complexity Analysis​

ApproachTimeSpace
Backtracking$O(n! or 2^n)$$O(n)$
Bit Manipulation$O(n) or O(1)$$O(1)$

Interview Tips​

Key Points
  • Discuss the brute force approach first, then optimize. Explain your thought process.
  • Identify pruning conditions early to avoid exploring invalid branches.
  • LeetCode provides 2 hint(s) for this problem β€” try solving without them first.
πŸ’‘ Hints

Hint 1: You can try all combinations and keep mask of characters you have.

Hint 2: You can use DP.