LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/
题目:
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr 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: 26
Constraints:
1 <= arr.length <= 161 <= arr[i].length <= 26arr[i]contains only lower case English letters.
题解:
In the arr, s could contain duplicate character. These strings with duplicate characters can't be used.
Have a list to maintain all previous bit masks. When checking a new string s, check all previous bit masks, if there is not intersection, then s could be used.
Update the maximum result and add the new bitmast into list.
Time Complexity: expontential.
Space: expontential.
AC Java:
class Solution {
public int maxLength(List<String> arr) {
int res = 0;
List<Integer> candidates = new ArrayList<>();
candidates.add(0);
for(String s : arr){
int dup = 0;
int sBit = 0;
for(char c : s.toCharArray()){
dup |= sBit & 1<<(c-'a');
sBit |= 1<<(c-'a');
}
if(dup > 0){
continue;
}
for(int i = 0; i<candidates.size(); i++){
if((candidates.get(i) & sBit) > 0){
continue;
}
candidates.add(candidates.get(i) | sBit);
res = Math.max(res, Integer.bitCount(candidates.get(i) | sBit));
}
}
return res;
}
}
LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters的更多相关文章
- 【leetcode】1239. Maximum Length of a Concatenated String with Unique Characters
题目如下: Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have ...
- [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- LN : leetcode 646 Maximum Length of Pair Chain
lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...
- [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- [LeetCode]Maximum Length of Repeated Subarray
Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...
- Maximum length of a table name in MySQL
http://dev.mysql.com/doc/refman/5.7/en/identifiers.html The following table describes the maximum le ...
随机推荐
- MySQL单机优化---分表、分区、分库
一.分表: 水平分表:根据条件把数据分为N个表(例如:商品表中有月份列,则可以按月份进行水平分表). 使用场景:一张表中数据太多,查询效率太慢. 当需要同时查询被水平分表的多张表时: 在两条SQL语句 ...
- C++函数形参与实参交换
c++中函数的实参传递到形参的值是单向的,改变形参并不会影响实参. #include <iostream> using namespace std; void swap(int a, in ...
- 第2课,python while循环的使用
引言: 上次课学习了python turtle库的基本使用,向前向后和转向.本次课需要画多个图形,简单的东西多起来就变得不简单了. 0/1是简单的,但却能组成丰富多彩的多媒体世界. 课程内容: 1. ...
- python_并发与通信
独立的进程内存空间与共享的服务器进程空间 知识点一: 进程间通信的限制 进程是独立的,互不干扰的独立内存空间我们想不能修改变量但是,深层次问题是,这个进程与那个进程完全失去了联系 import mul ...
- mysql 查询导出 excel 中文乱码 解决 --default-character-set=gbk
mysql --default-character-set=gbk -uroot -p -D open_fusion -e " select * from table1 " ...
- 遍历切片slice,结构体struct,映射map,interface{}的属性和值
1 前言 说明:interface{}必须是前三者类型 2 代码 /** * @Author: FB * @Description: * @File: testOutput.go * @Version ...
- DOS之del命令
基本 del命令是用来删除一个或多个文件的,删除文件夹的话还要用rd命令. 举个栗子: 例如我们要删除C盘中的a.txt,我们就可以 del a.txt 也可以同时删除多个,用空格,逗号或分号分开文件 ...
- 捡回reset的未提交修改
使用 Reflog 如果一開始沒有記下來 Commit 的 SHA-1 值也沒關係,Git 裡有個 reflog 指令有保留一些紀錄.再次借用上個章節的例子,但這次我改用 --hard 模式來進行 r ...
- 在Linux系统上安装Spring boot应用
Unix/Linux 服务 systemd 服务 操作过程 1. 安装了JDK的centOS7虚拟机 注意下载linux版本JDK的时候不能直接通过wget这种直接链接下载,否则会解压不成功,应该打开 ...
- python之路第一天
2018-07-11星期三 创建自己的博客(博客园): 登陆 我的博客 随笔:所有人在博客中都能看见的文章 文章:别人看不见,只能URL访问--我把网页地址发给你,你才能看到 日志:别人看不到,URL ...