#1239. Maximum Length of a Concatenated String with Unique Characters
function isUnique(combination) {
return new Set(combination).size === combination.length;
}
function dfs(index, current, arr) {
if (index === arr.length) {
return isUnique(current) ? current.length : 0;
}
// Include the current string
const includeCurrent = dfs(index + 1, current + arr[index], arr);
// Exclude the current string
const excludeCurrent = dfs(index + 1, current, arr);
return Math.max(includeCurrent, excludeCurrent);
}
function maxLength(arr) {
return dfs(0, "", arr);
}
I won't lie, I don't understand how the code solves the question. I guess I'd watch a tutorial on that.
ย