题目描述:

自己的提交:O(2**n∗n∗m),m 为字符串长度

class Solution:
def maxLength(self, arr: List[str]) -> int:
from collections import Counter
if not arr:return
res = set()
n = len(arr)
def helper(idx, tmp):
if len(tmp)>:
return
count = Counter(tmp)
if all(i== for i in count.values()):
res.add(tmp)
for i in range(idx, n):
helper(i+, tmp + arr[i])
helper(, "")
maxi =
# print(res)
for i in res:
maxi = max(maxi,len(i))
return maxi

另:动态规划

from collections import Counter
class Solution:
def sToNum(self, s):
ans =
for c in s:
ans |= <<(ord(c) - ord("a"))
return ans
def count(self, num):
ans =
while num:
num -= num&(-num)
ans +=
return ans
def maxLength(self, A):
A = [word for word in A if len(word) == len(set(word))]
sA = [self.sToNum(word) for word in A] aSet = set([])
for word in sA:
newS = set(aSet)
for s in aSet:
if not (s & word):
newS.add(s | word)
aSet = newS
# print (aSet)
return max(self.count(word) for word in aSet)

leetcode-160周赛-5240-串联字符串的最大长度的更多相关文章

  1. Leetcode 1239. 串联字符串的最大长度

    地址 https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/s ...

  2. LeetCode1239串联字符串的最大长度

    题目 给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串,如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解. 请返回所有可行解 s 中最长长度. 解题 ...

  3. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  4. Leetcode:003 无重复字符串

    Leetcode:003 无重复字符串 关键点:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度.示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复 ...

  5. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  6. LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  7. 【python】Leetcode每日一题-扰乱字符串

    [python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...

  8. Leetcode 680.验证回文字符串

    验证回文字符串 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...

  9. 力扣(LeetCode)验证回文字符串II 个人题解

    给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: ...

随机推荐

  1. jQuery判断checkbox是否选中的4种方法

    方法一: ).checked) { // do something } 方法二: if($('#checkbox-id').is(':checked')) { // do something } 方法 ...

  2. eclipse中服务器找不到项目怎么解决

    在我们运行项目前,都需要将项目部署到tomcat上,但是有时我们会遇到这种情况:项目明明存在,但是eclipse中tomcat的add and remove找不到项目,无法部署,那么这个问题该如何解决 ...

  3. shell 读取文件的每一行

    1.使用while #!/bin/bash while read line do echo $line done < file.txt #!/bin/bash cat file.txt | wh ...

  4. hdu 6092 Rikka with Subset (集合计数,01背包)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  5. jdbc 事物 commit 和rollback方法

    package transaction; import jdbc.utils.*; import java.sql.Connection; import java.sql.PreparedStatem ...

  6. oracle中解决中文乱码问题

    中文乱码问题解决 1.查看服务器端编码 select userenv('language') from dual; 查到结果是: USERENV('LANGUAGE') --------------- ...

  7. Java Software Engineer Skill Map

    # Java Software Engineer Skill Map## Basic### Core Java- Java The Complete Reference Ninth Edition.p ...

  8. python format函数的使用

    转载自:http://www.cnblogs.com/kaituorensheng/p/5709970.html python自2.6后,新增了一种格式化字符串函数str.format(),威力十足, ...

  9. HTML-参考手册: 按字母顺序排列

    ylbtech-HTML-参考手册: 按字母顺序排列 1.返回顶部 1. 按字母顺序排列 New : HTML5新标签 标签 描述 <!--...--> 定义注释 <!DOCTYPE ...

  10. 3. Node_export安装部署

    首先我们要知道什么是Node_export?因为Prometheus本身不具备监控功能,我们要通过Prometheus收集数据,需要安装对应的export.如Node_export用于监控服务器状态, ...