522. 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].
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的更多相关文章
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【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 ...
- [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- [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 ...
- LeetCode Longest Uncommon Subsequence II
原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- Leetcode Longest Uncommon Subsequence I
原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description 题目: Given a group ...
- Longest Uncommon Subsequence I
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...
随机推荐
- 关于 Web Api 2 认证与授权
认证与授权 认证与授权,Authentication and Authorize,这个是两个不同的事.认证是对访问身份进行确认,如验证用户名和密码,而授权是在认证之后,判断是否具有权限进行某操作,如 ...
- XAML中用一字符即可展示漂亮的图型
XAML中用一字符即可展示漂亮的图型 例如:Symbol Icon: People http://www.geekchamp.com/icon-explorer/action-icons/icon?c ...
- Selenium+python入门
在 WebDriver 中, 将这些关于鼠标操作的方法封装在 ActionChains 类提供 ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionCh ...
- 第一个C#语言
第一个C#程序 .NET和C#的区别 1. C#只是.NET的一部分,.NET不止包含C# 2. C#是一种程序语言,.NET是一个平台.框架 IDE环境:vs 2012 VS2012的窗口结构 ...
- Java第2章笔记
1.什么是变量:在程序运行过程中它的值是允许改变的量 2.java中常用的数据类型分为四类八种 第一类:整型 int(整数类型) byte(字节类型) short(短整形) ...
- mysql 执行计划分析三看, explain,profiling,optimizer_trace
http://blog.csdn.net/xj626852095/article/details/52767963 step 1 使用explain 查看执行计划, 5.6后可以加参数 explain ...
- html-day04
html-day04 1.html属性的弊端 1.完成相同的功能需要不同的属性支持 2.可维护性不高2.CSS 1.什么是CSS Cascading Style Sheet 层叠样式表.级联样式表.样 ...
- 字符串算法hash
思路:给字符串做一个映射,两个元素相同,则他们的hash值必定相同. 注意:hash表必须是unsigned int类型,保证每个映射都是正数. 例题: Description 给出两个字符串W和T, ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) D 多重排列 + 反向01背包 + 离线处理
https://codeforces.com/contest/1111/problem/D 多重排列 + 反向01背包 题意: 给你一个字符串(n<=1e5,n为偶数),有q个询问,每次询问两个 ...
- Codeforces Round #538 (Div. 2) C 数论 + 求b进制后缀零
https://codeforces.com/contest/1114/problem/C 题意 给你一个数n(<=1e8),要你求出n!在b进制下的后缀零个数(b<=1e12) 题解 a ...