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

这道题是之前那道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 最长非共同子序列之二的更多相关文章

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

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

  2. LeetCode Longest Uncommon Subsequence II

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

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

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

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

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

  5. Leetcode Longest Uncommon Subsequence I

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

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

  7. 522. Longest Uncommon Subsequence II

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

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

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

  9. 【leetcode】522. Longest Uncommon Subsequence II

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

随机推荐

  1. Jmeter 前置处理器 BeanShell_PreProcessor 适用思考

    首先摘抄一段官方文档的话: Before invoking the script, some variables are set up in the BeanShell interpreter: lo ...

  2. java基础(5)----面向对象

    编程思想: 简单的说一下,我们学习编程,最重要的就是要有编程思想,而编程思想无非就是面向过程和面向对象,以下谈谈我对编程思想的理解. 面向过程: 从过程入手,第一步,第二步--.借助过程与过程的配合, ...

  3. Mysql加密解密随机函数

    MD5(str) md5加密 SELECT MD5('hello') 5d41402abc4b2a76b9719d911017c592 sha(str) sha加密 SELECT SHA('hello ...

  4. JavaScript(第二十六天)【表单处理】

    为了分担服务器处理表单的压力,JavaScript提供了一些解决方案,从而大大打破了处处依赖服务器的局面.   一.表单介绍 在HTML中,表单是由<form>元素来表示的,而在JavaS ...

  5. JavaScript(第十四天)【面向对象和原型】

    学习要点: 1.学习条件 2.创建对象 3.原型 4.继承 ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标志,那就是类的概念,而通过类可以创建 ...

  6. C语言——第十四、十五周作业

    题目 题目一:交换最小值和最大值 1.实验代码 #include<stdio.h> int main() { ; int i , n; int a[N]; int x , y; scanf ...

  7. 网页设计入门<一>

    俗话说:技多不压身.实习周,网页设计是之一,边学边总结... 本次网页设计基于Adobe Dreamweaver CS6开发平台,根据实习老师的暴力指导,为什么说暴力呢? 没有基础,没有预告,打开软件 ...

  8. python学习笔记-问题

    1.字典按照值进行排序输出 2.返回函数-闭包的使用

  9. Beta冲刺Day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  10. IOS webview iframe 宽度超出屏幕解决方案

    IOS 真机webview中,iframe 却不能很好地适应屏幕大小,总是超出屏幕尺寸,需要左右滚动才能看到完整页面. <div style="overflow: auto;-webk ...