原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description

题目:

Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.

Example 1:

Input: "aba", "cdc", "eae"
Output: 3

Note:

  1. All the given strings' lengths will not exceed 10.
  2. The length of the given list will be in the range of [2, 50].

题解:

对每一个string取出所有可能的substring 放到同一个map里计数. 找出计数为1的最长substring长度.

Time Complexity: O(n * 2^k). n = strs.length, k 是longest string的长度. 每一个s有2^s.length()个substring.

Space: O(k). stack space.

AC Java:

 public class Solution {
public int findLUSlength(String[] strs) {
if(strs == null || strs.length == 0){
return -1;
} HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(String s : strs){
for(String subStr : getSubsequences(s)){
hm.put(subStr, hm.getOrDefault(subStr, 0)+1);
}
} int res = -1;
for(Map.Entry<String, Integer> entry : hm.entrySet()){
if(entry.getValue() == 1){
res = Math.max(res, entry.getKey().length());
}
}
return res;
} private HashSet<String> getSubsequences(String s){
HashSet<String> hs = new HashSet<String>(); if(s.length() == 0){
hs.add("");
return hs;
} HashSet<String> subHs = getSubsequences(s.substring(1));
hs.addAll(subHs);
for(String str : subHs){
hs.add(s.charAt(0)+str);
}
return hs;
}
}

类似Longest Uncommon Subsequence I.

LeetCode Longest Uncommon Subsequence II的更多相关文章

  1. [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  2. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  3. Leetcode Longest Uncommon Subsequence I

    原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description 题目: Given a group ...

  4. [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一

    Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...

  5. [Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  6. 522. Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  7. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  8. 【leetcode】522. Longest Uncommon Subsequence II

    题目如下: 解题思路:因为given list长度最多是50,我的解法就比较随意了,直接用一个嵌套的循环,判断数组中每个元素是否是其他的subsequence,最后找出不属于任何元素subsequen ...

  9. 522 Longest Uncommon Subsequence II 最长特殊序列 II

    详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...

随机推荐

  1. iOS 统计项目代码行数

    最近去面试 对面的"他" 问我其中一个问题 是 "你的项目代码量是多少?" 当时的确有点蒙圈, 我可以从整个项目打包的角度考虑项目大小,我还真没想过到底我的项目 ...

  2. $用python实现快速排序算法

    本文主要介绍用python实现基本的快速排序算法,体会一下python的快排代码可以写得多么简洁. 1. 三言两语概括算法核心思想 先从待排序的数组中找出一个数作为基准数(取第一个数即可),然后将原来 ...

  3. 键盘没有Home键和End键的完美解决办法

    最近新入手一个笔记本,发现键盘没有Home/End,这两个键虽然不是必用,但也是用顺手了,特别是选择一行,到行首,行尾的时候甚是方便 作为一枚程序员,怎么能够妥协? 于是开始研究 方案一 通过观察笔记 ...

  4. Linux基本命令 vim命令(二)

    Linux Vim显示行号 在命令模式下输入" : " 进入编辑模式后执行 set nu 命令 即可显示每一行的行号,如果想要取消行号,则再次输入":set nonu&q ...

  5. Python编程-多线程

    一.python并发编程之多线程 1.threading模块 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 1.1 开启线程的 ...

  6. Hadoop单机搭建

    单机Hadoop搭建 1.下载hadoop-2.7.3.tar.gz http://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-2.7.3/h ...

  7. 一、安装虚拟机,配置ip地址

    一.安装linux 注意点: 一.选择最小化安装的时候,要自定义安装软件,必须要安装下面的 如果没有安装上面的,需要用下面的命令来查询安装 如果没有安装就会出现各种问题 二.分区简单介绍 1.至少要一 ...

  8. MyEclipse Could not create the view: An unexpected exception was thrown解决方案

    问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...

  9. RabbitMQ自动补偿机制(消费者)及幂等问题

    如果消费者 运行时候 报错了 package com.toov5.msg.SMS; import org.springframework.amqp.rabbit.annotation.RabbitHa ...

  10. springcloud-声明式调用服务Feign

    springcloud项目例子:链接:https://pan.baidu.com/s/1O1PKrdvrq5c8sQUb7dQ5Pg 密码:ynir 作用: 基于Netflix Feign整合了Rib ...