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 ...
随机推荐
- tms web core 与 kbmmw 第一次亲密接触
最近,tms 经过1年多,集合了数十名高手大牛,开发出了一个跨时代的产品,就是tms web core. 具体的介绍详见官网,https://www.tmssoftware.com/site/tmsw ...
- 同时安装python2.7和python3.5
同时安装python2.7和python3.5,并配置sublime ctrl+B选择运行python版本 安装python 首先是安装两个版本的python,并配置相应的环境变量 1.在下载安装好P ...
- chrome 字体太浅,如何设置
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-directwrite-for-ui
- ON_UPDATE_COMMAND_UI和ON_COMMAND有什么区别?
区别如下: UPDATE_COMMAND_UI表示处理菜单对应的用户界面显示状态. COMMAND表示处理该菜单对应的功能. 传统SDK程序要改变选单命令项状态,可以呼叫EnableMenuItem或 ...
- VC6.0编译器设置
主要通过VC的菜单项Project->Settings->C/C++页来完成.我们可以看到这一页的最下面Project Options中的内容,一般如下:/nologo /MDd /W3 ...
- Java输入输出技术
输入输出分类 输入流,相对计算机来说是输入的,例如鼠标键盘操作,设备给计算机的信息 输出流,相对计算机来说是输出的,例如屏幕显示,计算机给设备的信息. 具体分类 基本流,I ...
- 正则表达式Regular expressions
根据某种匹配模式来寻找strings中的某些单词 举例:如果我们想要找到字符串The dog chased the cat中单词 the,我们可以使用下面的正则表达式: /the/gi 我们可以把这个 ...
- day26(分页查询)
分页查询思路 问题: 服务器向浏览器想用数据很多的时候可以对数据进行封装. domain层 封装数据 package com.baidu.domain; import java.util.List; ...
- c语言:辗转相除法求最大公约数、最小公倍数
辗转相除法,又称欧几里得算法.两个正整数a和b(a>b),它们的最大公约数等于余数c和较小的数b之间的最大公约数.最小公倍数=两数之积/最大公约数 #include <stdio.h> ...
- (最短路 Floyd diskstra prim)Frogger --POJ--2253
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...