522. Longest Uncommon Subsequence II

本文探讨了在一组字符串中寻找最长不同子序列的问题,详细解释了一种通过排序和检查子序列的方法,并提供了Java实现代码。该方法首先消除重复项,然后从最长的字符串开始,检查是否为其他更短字符串的子序列。

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

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/10732789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值