LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
2024-10-20 19:01:23
原题链接在这里: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 <= 16
1 <= arr[i].length <= 26
arr[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;
}
}
最新文章
- Mac >; MacBook Pro的移动硬盘方案
- 微信小程序 wx.uploadFile在安卓手机上面the same task is working问题解决
- 编译预处理命令--define和ifdef的使用
- 移动web开发--meta 之 viewport
- [javascript]event属性
- Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN(http://smartblack.iteye.com/blog/1129097)
- oracle日期函数集锦
- CocoaPods 建立私有仓库
- MongoDB分片原理篇
- 阿里云服务器部署php的laravel项目,在阿里云买ECS 搭建 Linux+Nginx+Mysql+PHP环境的
- push_back和emplace_back的区别
- EBS DBA指南笔记(三)
- 专治编译器编辑器vscode中文乱码输出 win10 配置系统默认utf-8编码
- Linux简易APR内存池学习笔记(带源码和实例)
- 《C#并发编程经典实例》学习笔记-进程(process)和线程(thread)
- 使用VSTS的Git进行版本控制(四)——在Visual Studio中管理分支
- Spark基础脚本入门实践3:Pair RDD开发
- EF访问数据库报“ExecuteReader 要求已打开且可用的 Connection。连接的当前状态为已关闭。”错误
- PGIS下载离线地图 SQLite+WPF
- Caffe 深度学习框架上手教程
热门文章
- JAVA 8 的新特性
- PAT甲级1006水题飘过
- Python 入门(1):hello world 到流程控制
- jwt的思考
- mysql执行出错:Table &#39;k_user&#39; is read only
- Asp.net MVC 之ActionResult
- Java调用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection调用Http/Https接口
- Lipo移除ORC架构
- 微信小程序项目开发实战:用WePY、mpvue、Taro打造高效的小程序》(笔记4)支持React.js语法的Taro框架
- KVM on CubieTruck 原理以及网络性能相关思考