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

Approach #1: Simulate. [Java]

class Solution {

    public int findLUSlength(String[] strs) {
Arrays.sort(strs, new Comparator<String>() {
public int compare(String o1, String o2) {
return o2.length() - o1.length();
}
}); Set<String> duplicates = getDuplicates(strs);
for (int i = 0; i < strs.length; ++i) {
if (!duplicates.contains(strs[i])) {
if (i == 0) return strs[0].length();
for (int j = 0; j < i; ++j) {
if (isSubsequence(strs[j], strs[i])) break;
if (j == i - 1) return strs[i].length();
}
}
} return -1;
} boolean isSubsequence(String a, String b) {
int i = 0, j = 0;
while (i < a.length() && j < b.length()) {
if (a.charAt(i) == b.charAt(j)) ++j;
++i;
}
return j == b.length();
} Set<String> getDuplicates(String[] strs) {
Set<String> set = new HashSet<String>();
Set<String> duplicates = new HashSet<String>();
for (String str : strs) {
if (set.contains(str)) duplicates.add(str);
set.add(str);
}
return duplicates;
}
}

  

Analysis:

Sort the string in the reverse order. If there is not duplicates in the array, then the longest string is the answer.

But if there are duplicates, and if the longset string is not the answer, then we need to check other strings. But the smaller string can be subsequence of the bigger string. For this reason, we need to check if the string is a subsquence of all the strings bigger than itself. If not, that is the answer.

Reference:

https://leetcode.com/problems/longest-uncommon-subsequence-ii/discuss/99443/Java(15ms)-Sort-%2B-check-subsequence

522. Longest Uncommon Subsequence II的更多相关文章

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

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

  2. 【leetcode】522. Longest Uncommon Subsequence II

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

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

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

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

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

  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. LeetCode Longest Uncommon Subsequence II

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

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

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

  8. Leetcode Longest Uncommon Subsequence I

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

  9. Longest Uncommon Subsequence I

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

随机推荐

  1. 关于Web应用开发流程的总结

    以下内容为个人工作总结,如果不当,谢谢指出错误. 假设最简单的情况,一个开发人员,开发所有的代码,一个测试人员.一个测试的服务器,一个生产的服务器. 开发人员需要为公司开发一个项目,开发人员首先分析产 ...

  2. 【转载】 Jointwave零延时视频传输for FPGA/ASIC进入军工领域

    半导体知识产权H.264/H.265 硅IP核供应商Jointwave公司的发布了一系列视频编解码RTL IP核,已经成功应用于军事工业领域的指挥作战,无人机UAV控制,航空和航天摄像机,视频记录黑匣 ...

  3. 59.加载Viewcontroller的几种方法(添加导航,解决xib里面空间不显示问题)

    // 一.根据StoryboardID(需要在Storyboard设置),通过ViewController所在的Storyboard来加载: UIStoryboard *storyboard = [U ...

  4. 牛客训练三:处女座和小姐姐(三)(数位dp)

    题目链接:传送门 思路:数位dp的记忆化搜索模板 从高位向低位枚举,逐位确定每一位的6的个数,dp[i][s]表示处理到第i条边,状态为s时的数字的个数. 注意,要使用long long类型. #in ...

  5. windows下Oracle数据库完全删除

    1.1   停止所有oracle的服务 1.2   删除安装路径 app及其下所有文件 1.3   删除注册表 regedit 进入 在下列列表中找到与oracle相关的注册表项删除 1.HKEY_L ...

  6. 关于对话框不能响应OnKeyDown和OnChar函数的一些说明

    (1)现象  在MFC的对话框中,映射了WM_CHAR和WM_KEYDOWN消息响应函数后,还是不能响应OnKeyDown和OnChar. (2)原因  因为MFC在进行设计的时候,这两个消息被对话框 ...

  7. Mybatis之动态构建SQL语句(转)

    https://www.cnblogs.com/zhangminghui/p/4903351.html

  8. Keepalived+Nginx高可用架构配置

    1.yum install -y libnfnetlink-devel2.yum -y install libnl libnl-devel 3.yum -y install openssl-devel ...

  9. gitlab常用命令

    进入本地仓库访问位置之后执行命令 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote ...

  10. REST WebService与SOAP WebService的比较

    在SOA的基础技术实现方式中WebService占据了很重要的地位,通常我们提到WebService第一想法就是SOAP消息在各种传输协议上交互.近几年REST的思想伴随着SOA逐渐被大家接受,同时各 ...