[LeetCode] 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:

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

题意:找出一个字符串数组中最长的特殊字符串

特殊字符串指的是,它不是任何非它的子序列串,子序列串就不解释了

其一,如果一个字符串的长度大于另一个字符串,那么它一定不是另一个的子序列串,

其二,我们找到最长的那个可以从最长的开始判断。

所以综上两点,我们可以想到对其先排序,从大到小按照字符串的长度,但是有个问题,如果是等长的,那么所有和它等长的我们都需要去判断,

所以我们需要维护一个数组,其中包含了需要和当前元素判断的数组

class Solution {
    private boolean ifSubString(String parent, String sub) {
        int i = 0;
        int j = 0;
        while (i < parent.length() && j < sub.length()) {
            if (parent.charAt(i) == sub.charAt(j)) {
                i++;
                j++;
            } else
                i++;
        }
        if (j >= sub.length())
            return true;
        return false;
    }
    public int findLUSlength(String[] strs) {
        if (strs.length == 0)
            return 0;
        Map<Integer, ArrayList<String>> map = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (o1.intValue() > o2.intValue())
                    return -1;
                if (o1.intValue() < o2.intValue())
                    return 1;
                return 0;
            }
        });
        for (int i = 0; i < strs.length; i++) {
            int lth = strs[i].length();
            if (map.containsKey(lth)) {
                ArrayList list = map.get(lth);
                list.add(strs[i]);
                map.put(lth, list);
            } else {
                ArrayList<String> list = new ArrayList<>();
                list.add(strs[i]);
                map.put(lth, list);
            }
        }

        ArrayList<String> res = new ArrayList<>();
        for (Map.Entry entry : map.entrySet()) {
            ArrayList<String>list = (ArrayList) entry.getValue();
            for (int i = 0; i < list.size(); i++)
                res.add(list.get(i));

            if (res.size() == 1)
                return res.get(0).length();

            for (int i = 0; i < res.size(); i++) {
                boolean flag = true;
                for (int j = 0; j < res.size(); j++) {
                    if (i == j)
                        continue;
                    if (ifSubString(res.get(j), res.get(i))) {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                    return (int) entry.getKey();
            }
        }
        return -1;
    }
}

原文地址:https://www.cnblogs.com/Moriarty-cx/p/9911522.html

时间: 2024-11-09 12:46:23

[LeetCode] 522. Longest Uncommon Subsequence II的相关文章

LeetCode Longest Uncommon Subsequence II

原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: 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 o

[LeetCode] 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 stri

LeetCode 521. Longest Uncommon Subsequence I

question: Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be

LeetCode 521 Longest Uncommon Subsequence I 解题报告

题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be anysu

[LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subseq

LeetCode: 521 Longest Uncommon Subsequence I(easy)

题目: anysubsequence 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

Longest Uncommon Subsequence I

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subseq

521. Longest Uncommon Subsequence I

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subseq

521. Longest Uncommon Subsequence I (最长不寻常子序列) by Python

题目: Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any su