• [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;
        }
    }
  • 相关阅读:
    如何解决列表框控件宽度不够的问题
    在windows Forms程序里面实现文件上传
    TFS的一些信息
    百钱百鸡问题
    如何移动SQL SERVER的系统数据库
    数据分页技术
    如何在报表中直接使用数据库中存储的图片
    重新注册MSJetOledb 4.0引擎
    2007 Microsoft Office Servers 已知问题/自述文件
    使用For XML与XSL(XSLT)配套快速输出查询结果到Web页面
  • 原文地址:https://www.cnblogs.com/Moriarty-cx/p/9911522.html
Copyright © 2020-2023  润新知