Maximum Length of a Concatenated String with Unique Characters

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 keeps the original order but may drop any elements.

30:00

Maximum Length of a Concatenated String with Unique Characters
medium
Free written solution
Topics
Companies

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 keeps the original order but may drop any elements.

Example 1:
Input: {"arr":["un","iq","ue"]}
Output: 4
Constraints:
  • 1≤arr.length≤161 \leq arr.length \leq 16

  • 1≤arr[i].length≤261 \leq arr[i].length \leq 26

  • arr[i] contains only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":["un","iq","ue"]}

Start with one letter set: the empty string. Every reachable concatenation will be built by extending a set already in the list.

arr

un
iq
ue

letter masks (bit 0 = a ... bit 25 = z)

set & word = ?

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

compared set

current word

shared letter = conflict

valid sets so far (1)

best = 0

""0
Variables
VariableValue
sets1
best0
DepthFunction Call
Stack empty
0/19