原题链接在这里: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;
}
}

LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters的更多相关文章

  1. 【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 ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. LeetCode Maximum Length of Pair Chain

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n  ...

  6. LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

    718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...

  7. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  8. [LeetCode]Maximum Length of Repeated Subarray

    Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...

  9. 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 ...

随机推荐

  1. B站动态转发抽奖脚本+教程

    运行python脚本需要的条件: 1.连通的网络 2.已安装Python2并配置环境变量 3.Python脚本源码 环境搭建: 网络就不用我说了("'▽'")  那么下面我们来安装 ...

  2. idea 用鼠标滚轮调整代码文字大小

    File > Settings > Keymap > Editor Actions 下,我们可以找到 “Decrease Font Size”和“Increase Font Size ...

  3. C#——零散学习1

    C#——零散学习1 //结构体(与C语言相似) struct Position { public float x; public float y;         //不一定需要把结构体成员设置为pu ...

  4. Angular复习笔记5-指令

    Angular复习笔记5-指令 在Angular中,指令是一个重要的概念,它作用在特定的DOM元素上,可以扩展这个元素的功能,为元素增加新的行为.本质上,组件可以被理解为一种带有视图的指令.组件继承自 ...

  5. Deployment.spec.selector.matchLables实验解释

    原文:https://cloud.tencent.com/developer/article/1394657 Deployment.spec.selector.matchLables实验解释 作者: ...

  6. Python进阶----网络通信基础 ,OSI七层协议() ,UDP和TCP的区别 , TCP/IP协议(三次握手,四次挥手)

    Python进阶----网络通信基础 ,OSI七层协议() ,UDP和TCP的区别 , TCP/IP协议(三次握手,四次挥手) 一丶CS/BS 架构 C/S: 客户端/服务器    定义:       ...

  7. 【面试突击】- Java面试总则

    Java基础 1.Map.Set.List集合差别及联系详解 2.HashSet类是如何实现添加元素保证不重复的 3.HashMap 是线程安全的吗,为什么不是线程安全的(最好画图说明多线程环境下不安 ...

  8. HTML学习摘要1

    在http://www.w3school.com.cn/ 学习前端知识,利用暑假,自主学习以拓展知识面 DAY 1 HTML 不是一种编程语言,而是一种标记语言 (markup language) 标 ...

  9. JS-练习题

    1.foo()结果 function foo() { bar.apply(null, arguments); } function bar(){ console.log(arguments); } f ...

  10. 个人项目 wc(java实现)

    一.Github网址: https://github.com/Clarazhangbw/Wc.exe 二.PSP表 PSP2.1 Personal Software Process Stages 预估 ...