[LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二
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].
这道题是之前那道Longest Uncommon Subsequence I的拓展,那道题因为只有两个字符串为大家所不屑。那么这道题有多个字符串,这次大家满足了吧。令我吃惊的是,这次的OJ异常的大度,连暴力搜索的解法也让过,那么还等什么,无脑暴力破解吧。遍历所有的字符串,对于每个遍历到的字符串,再和所有的其他的字符串比较,看是不是某一个字符串的子序列,如果都不是的话,那么当前字符串就是一个非共同子序列,用其长度来更新结果res,参见代码如下:
解法一:
class Solution {
public:
int findLUSlength(vector<string>& strs) {
int res = -, j = , n = strs.size();
for (int i = ; i < n; ++i) {
for (j = ; j < n; ++j) {
if (i == j) continue;
if (checkSubs(strs[i], strs[j])) break;
}
if (j == n) res = max(res, (int)strs[i].size());
}
return res;
}
int checkSubs(string subs, string str) {
int i = ;
for (char c : str) {
if (c == subs[i]) ++i;
if (i == subs.size()) break;
}
return i == subs.size();
}
};
下面这种解法使用一些博主能想到的优化手段,首先我们给字符串按长度来排序,将长度大的放到前面,这样我们如果找到了非共同子序列,那么直接返回其长度即可,因为当前找到的肯定是最长的。然后我们用一个集合来记录已经遍历过的字符串,利用集合的去重复特性,这样在有大量的重复字符串的时候可以提高效率,然后我们开始遍历字符串,对于当前遍历到的字符串,我们和集合中的所有字符串相比,看其是否是某个的子序列,如果都不是,说明当前的就是最长的非共同子序列。注意如果当前的字符串是集合中某个字符串的子序列,那么直接break出来,不用再和其他的比较了,这样在集合中有大量的字符串时可以提高效率,最后别忘了将遍历过的字符串加入集合中,参见代码如下:
解法二:
class Solution {
public:
int findLUSlength(vector<string>& strs) {
int n = strs.size();
unordered_set<string> s;
sort(strs.begin(), strs.end(), [](string a, string b){
if (a.size() == b.size()) return a > b;
return a.size() > b.size();
});
for (int i = ; i < n; ++i) {
if (i == n - || strs[i] != strs[i + ]) {
bool found = true;
for (auto a : s) {
int j = ;
for (char c : a) {
if (c == strs[i][j]) ++j;
if (j == strs[i].size()) break;
}
if (j == strs[i].size()) {
found = false;
break;
}
}
if (found) return strs[i].size();
}
s.insert(strs[i]);
}
return -;
}
};
类似题目:
Longest Uncommon Subsequence I
参考资料:
https://discuss.leetcode.com/topic/85033/checking-subsequence-without-hashing
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二的更多相关文章
- [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...
- LeetCode Longest Uncommon Subsequence II
原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list ...
- 522 Longest Uncommon Subsequence II 最长特殊序列 II
详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...
- 【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 ...
- [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 ...
随机推荐
- Repeating Decimals UVA - 202
The / repeats indefinitely with no intervening digits. In fact, the decimal expansion of every ratio ...
- MIPCMS V3.1.0 远程写入配置文件Getshell过程分析(附批量getshell脚本)
作者:i春秋作家--F0rmat 0×01 前言 今天翻了下CNVD,看到了一个MIPCMS的远程代码执行漏洞,然后就去官网下载了这个版本的源码研究了下.看下整体的结构,用的是thinkPHP的架 ...
- 使用IDEA配置Maven + SpringMVC + Mybatis 【一步一步踩坑详细配置完成】
PS:初学,想使用Maven配置一个SpringMVC的开发环境,照着网上的各种图文解说,配置了好久都没成功,有些写的不够详细,有些只有写一半,走了不少弯弯绕绕,踩了不少的坑,此文将正确配置成功的步骤 ...
- java通过数据库连接池链接oracle
开发工具:Eclipse J2EE 3.6 运行环境:jdk1.6 部署环境:Tomcat7 数据库连接池用的是dbcp,网上download下来的三个jar包. 把数据库连接池包和jdbc的包放到t ...
- JavaScript(第十七天)【浏览器检测】
由于每个浏览器都具有自己独到的扩展,所以在开发阶段来判断浏览器是一个非常重要的步骤.虽然浏览器开发商在公共接口方面投入了很多精力,努力的去支持最常用的公共功能:但在现实中,浏览器之间的差异,以及不同浏 ...
- 使用jmeter+ant进行接口自动化测试(数据驱动)之一:设计jmeter脚本
最近在做接口测试,因为公司有使用jmeter做接口测试的相关培训资料,所以还是先选择使用jmeter来批量管理接口,进行自动化测试.话不多说,进入正题: 1.使用csv文件保存接口测试用例,方便后期对 ...
- C语言程序设计(基础)- 第14、15周作业
从本周开始,将作业标记为学校自然周,而不是开课的周数. 要求一(25经验值) 完成14.15周的所有PTA中题目集. 注意1:一周两次pta作业,包括四次. 要求二(50经验值) 博客的具体书写内容和 ...
- 按指定id顺序查询
SELECT a.p,* FROM tb1 t,( as p union as p union as p union as p union as p ) a where t.id=a.id order ...
- 2017-2018-1 1623 bug终结者 冲刺004
bug终结者 冲刺004 by 20162322 朱娅霖 整体连接 简要说明 目前,我们已经完成了欢迎界面,主菜单界面,排行榜界面,选项界面,胜利界面,地板类.小人类.墙体类.箱子类和虚拟按键类. 主 ...
- 《高级软件测试》11.15.全组完成jira安装,开始任务的部分书写
今日任务完成情况如下: 小段:完成linux环境上jira的安装,并将jira的安装过程录制下来 小费:完成linux环境下jira的安装,开始部分任务的书写 小高:完成了jira的安装,并进一步熟悉 ...