【leetcode】1239. Maximum Length of a Concatenated String with Unique Characters
题目如下:
Given an array of strings
arr
. Strings
is a concatenation of a sub-sequence ofarr
which have unique characters.Return the maximum possible length of
s
.Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i]
contains only lower case English letters.
解题思路:首先过滤掉arr中有重复字符的元素,接下来就可以采用BFS的方法进行计算了。怎么判断两个字符串是否有相同字符呢?我的方法是把字符串转成二进制的方式,a对应2^0,c对应2^2,z对应2^25。这样的话,每一个字符串都可以计算出一个特征值,判断两个字符串是否有相同字符,只有判断两个特征值与操作的结果是否为0即可。
代码如下:
class Solution(object):
def maxLength(self, arr):
"""
:type arr: List[str]
:rtype: int
"""
arr_val = []
for i in arr:
val = 0
if len(set(list(i))) != len(i):continue
for j in i:
val |= 2**(ord(j) - ord('a'))
arr_val.append(val)
#print arr_val
queue = []
if len(arr_val) == 0:return 0
for (inx,val) in enumerate(arr_val):
queue.append((inx,val))
res = 0
while len(queue) > 0:
inx,val = queue.pop(0)
end = True
for i in range(inx+1,len(arr_val)):
if val & arr_val[i] != 0:continue
queue.append((i,val | arr_val[i]))
end = False
if end :
res = max(res,bin(val).count(''))
return res
【leetcode】1239. Maximum Length of a Concatenated String with Unique Characters的更多相关文章
- LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
随机推荐
- 华为HCNA乱学Round 4:RIP
- Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- 使用feign上传图片
1.添加依赖,支持SpringEncoder <dependency> <groupId>io.github.openfeign.form</groupId> &l ...
- codeforces 816B Karen and Coffee (差分思想)
题目链接 816B Karen and Coffee 题目分析 题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可 ...
- 版本控制器之SVN(一)
通常软件开发由多人协作开发,如果对代码文件.配置文件.文档等没有进行版本控制,将会出现很多问题: 备份多个版本,占用磁盘空间大 解决代码冲突困难 容易引发BUG 难于追溯问题代码的修改人和修改时间 难 ...
- ftp服务器上传下载共享文件
1 windows下搭建ftp服务器 https://blog.csdn.net/qq_34610293/article/details/79210539 搭建好之后浏览器输入 ftp://ip就可以 ...
- 099、如何访问Service (Swarm06)
参考https://www.cnblogs.com/CloudMan6/p/7909136.html 前面已经学习了如何部署Service吗,也验证了swarm的failover特性,下面我们要学 ...
- luogu P3620 [APIO/CTSC 2007]数据备份
luogu 首先如果一条线不是了连接的相邻两个位置一定不优,把它拆成若干连接相邻位置的线.所以现在问题是有\(n\)个物品,选\(k\)个,要求选的位置不能相邻,求最小总和 如果没有选的位置不能相邻这 ...
- lambda中FirstOrDefault和First
First()表示取集合中的第一个元素,如果集合为空,则抛异常. FirstOrDefault()表示取集合的第一个元素. 如果集合为空,且集合元素是引用类型,则返回null. 如果集合为空,且集合元 ...
- centos 7 Network 脚本
#!/bin/sh #主动启动网卡 interface=$() ifup $interface #获取当前网络信息 default_route=$(ip route show) default_int ...