#1239. Maximum Length of a Concatenated String with Unique Characters

ยท

1 min read

https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/description/?envType=daily-question&envId=2024-01-23

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.

ย