原题链接在这里: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. MyBatis参数条件查询传入的值为0时的判断

    MyBatis条件查询对字段判断是否为空一般为: <if test="testValue!=null and testValue != ''"> and test_va ...

  2. Word 插入脚注、尾注与题注 -- 视频教程(5)

    >> 视频教程链接:B站,速度快,清晰 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)

  3. 【题解】Luogu P5279 [ZJOI2019]麻将

    原题传送门 希望这题不会让你对麻将的热爱消失殆尽 我们珂以统计每种牌出现的次数,不需要统计是第几张牌 判一副牌能不能和,类似这道题 对于这题: 设\(f[i][j][k][0/1]\)表示前\(i\) ...

  4. ubuntu中安装python3和pip

    python3: 在ubuntu的包中,python的二代和三代版本的命名:二代:python,三代:python3 安装python3: sudo apt install python3 同理:pi ...

  5. Spring中的ApplicationListener的使用详解案例

    本文链接:https://blog.csdn.net/u010963948/article/details/83507185 1.ApplicationContext Spring的核心,Contex ...

  6. Flutter — IDE Shortcuts for Faster Development

    https://medium.com/flutter-community/flutter-ide-shortcuts-for-faster-development-2ef45c51085b If yo ...

  7. 2019-07-22 phpStudy配置虚拟主机

    1.右击 phpStudy ->[打开配置文件]->[vhosts-conf]: 2.在里面加入如下代码,并保存: NameVirtualHost *:80 <VirtualHost ...

  8. springmvc 注解二

    @SessionAttributes @sessionattributes注解应用到Controller上面,可以将Model中的属性同步到session作用域当中. SessionAttribute ...

  9. 【体系结构】有关Oracle SCN知识点的整理

    [体系结构]有关Oracle SCN知识点的整理 1  BLOG文档结构图   BLOG_Oracle_lhr_Oracle SCN的一点研究.pdf 2  前言部分 2.1  导读和注意事项 各位技 ...

  10. 【RAC】 RAC For W2K8R2 安装--结尾篇(十)

    [RAC] RAC For W2K8R2 安装--结尾篇(十) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其 ...