首页 技术 正文
技术 2022年11月13日
0 收藏 479 点赞 3,187 浏览 1965 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902