LeetCode Longest Uncommon Subsequence II
原题链接在这里: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:
- All the given strings' lengths will not exceed 10.
- 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的更多相关文章
- [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- Leetcode Longest Uncommon Subsequence I
原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description 题目: Given a group ...
- [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...
- [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 ...
- 522. Longest Uncommon Subsequence II
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【leetcode】522. Longest Uncommon Subsequence II
题目如下: 解题思路:因为given list长度最多是50,我的解法就比较随意了,直接用一个嵌套的循环,判断数组中每个元素是否是其他的subsequence,最后找出不属于任何元素subsequen ...
- 522 Longest Uncommon Subsequence II 最长特殊序列 II
详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...
随机推荐
- Vimium~让您的Chrome起飞
工欲善其事,必先利其器!撸起Vimium,我的Chrome就这么起飞了. 学起(了解几个快捷键即可)And撸起Vimium,想黑客一般在Chrome上飞起.Vimium常用快捷键(注:区分大小写)j, ...
- iOS 系统认知 debug distribution release 和 #ifdef DEBUG
debug:调试模式 有调试信息 线下 release: 无调试信息 经过了编译优化 发布 给用户使用的 线上模式 一般 工程项目 都是自带 上述两种配置结构 还有出现 distribution: ...
- Python3.4 用 pip 安装lxml时出现 “Unable to find vcvarsall.bat ”
我的python版本是Python 3.5 该问题的产生是在windows环境中,python 的 Setup需要调用一个vcvarsall.bat的文件,该文件需要安装c++编程环境才会有.网上的方 ...
- 让iOS项目允许使用http协议请求
苹果官方已经默认不让开发者使用不安全的http通信协议了,而是建议开发者使用安全的https协议.若我们还是需要使用http协议可以这样配置XCode: 1.打开info.plist文件 2.点击加号 ...
- java resources 红叉 Cannot change version of project facet Dynamic Web Module to 2.5
在使用maven导入项目的时候,markers提示Cannot change version of project facet Dynamic Web Module to 2.5,不能将工程转换为2. ...
- freemarker模板解析过程
例如:一个freemarker表达式<body> ${hello} </body>,会被解析成三个部分,分别是<body>${hello}</body> ...
- centos下安装Anaconda
第一步:将下载好的Anaconda2-4.1.1-Linux-x86_64.sh软件传到linux下 第二步:[hadoop@spark1 ~]$ cd Desktop #进入到该软件所在目录,我的放 ...
- JavaScript -- 节点操作, 事件触发, 表单伸缩
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- dataframe to sql
使用pandas的to_sql时,还涉及到一些sql数据库的操作 在网上找到一段代码,很方便使用.来源:blog.csdn.net/qcyfred/article/details/78085243?l ...
- Delphi调用Java类
1. Delphi XE7调用Java Class,JAR http://www.th7.cn/Program/delphi/201409/277888.shtml ZC: 文章中又提到:http:/ ...