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)
相关推荐
张人玉13 分钟前
VisionPro Blob、条码识别、OCR 结构化速记版
人工智能·算法·机器学习·vsionpro
愚者游世27 分钟前
力扣解决二进制&题型常用知识点梳理
c++·程序人生·算法·leetcode·职场和发展·改行学it
圣保罗的大教堂27 分钟前
leetcode 3640. 三段式数组 II 困难
leetcode
Geoking.30 分钟前
前缀和算法:从一道 LeetCode 题看区间求和优化思想
算法·leetcode·职场和发展
爱吃rabbit的mq33 分钟前
第7章 逻辑回归:二分类的基础
算法·分类·逻辑回归
DFT计算杂谈35 分钟前
VASP+Wannier90 计算位移电流和二次谐波SHG
java·服务器·前端·python·算法
执着25940 分钟前
力扣102、二叉树的层序遍历
数据结构·算法·leetcode
Tisfy43 分钟前
LeetCode 2976.转换字符串的最小成本 I:floyd算法(全源最短路)
算法·leetcode··floyd·题解
v_for_van1 小时前
力扣刷题记录4(无算法背景,纯C语言)
c语言·算法·leetcode
dazzle1 小时前
Python数据结构(十五):归并排序详解
数据结构·python·算法