leetcode - 1239. Maximum Length of a Concatenated String with Unique Characters

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.

Solution

Recursive

Recursive, for each string, use it or don't use it. We could only use it when the characters don't appear before.

Time complexity: o ( 2 n ) o(2^n) o(2n)

Space complexity: o ( n ) o(n) o(n)

DP

Use set in dp, every time check if current string could be added to the set.

Code

Recursive

python3 复制代码
class Solution:
    def maxLength(self, arr: List[str]) -> int:
        visited_char = {}
        def helper(index: int) -> int:
            if index >= len(arr):
                return 0
            res = 0
            could_use = True
            for each_c in arr[index]:
                if visited_char.get(each_c, 0) > 0:
                    could_use = False
                visited_char[each_c] = visited_char.get(each_c, 0) + 1
            if could_use:
                res = max(res, len(arr[index]) + helper(index + 1))
            # remove from visited_set, don't use
            for each_c in arr[index]:
                visited_char[each_c] -= 1
            res = max(res, helper(index + 1))
            return res
        return helper(0)

DP

python3 复制代码
class Solution:
    def maxLength(self, arr: List[str]) -> int:
        dp = [set()]
        for each_string in arr:
            string_set = set(each_string)
            if len(string_set) < len(each_string):
                continue
            prev_dp = dp[:]
            for prev_set in prev_dp:
                if string_set & prev_set:
                    continue
                else:
                    dp.append(string_set | prev_set)
        return max(len(each_set) for each_set in dp)
相关推荐
山登绝顶我为峰 3(^v^)339 分钟前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Two_brushes.2 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
森焱森4 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
QuantumStack6 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
写个博客6 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠6 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
hie988947 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
杰克尼7 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
.30-06Springfield8 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦8 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt