最长非公共子序列(既不相同也不重复)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.

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

解决:

①  暴力解决。遍历所有的字符串,对于每个遍历到的字符串,再和所有的其他的字符串比较,看是不是某一个字符串的子序列,如果都不是的话,那么当前字符串就是一个非共同子序列,用其长度来更新结果res。

class Solution { //11ms
    public int findLUSlength(String[] strs) {
        int res = -1;
        int j = 0;
        int len = strs.length;
        for (int i = 0;i < len;i ++){
            for (j = 0;j < len;j ++){
                if (i == j) continue;
                if (isSubs(strs[i],strs[j])) break;
            }
            if (j == len) res = Math.max(res,strs[i].length());
        }
        return res;
    }
    public boolean isSubs(String subs,String str){
        int i = 0;
        for (char c : str.toCharArray()){
            if (c == subs.charAt(i)) i ++;
            if (i == subs.length()) break;
        }
        return i == subs.length();
    }
}

② 按照字符串长度降序排列strs;遍历strs,如果str不是所有strs的独有子字符串,返回str的长度;如果没有找到独有字符串,返回-1。

class Solution { //12ms
    public int findLUSlength(String[] strs) {
        Arrays.sort(strs, new Comparator<String>() {//按照长度从大到小排序
            @Override
            public int compare(String o1, String o2) {
                return o2.length() - o1.length();
            }
        });
        for (int i = 0;i < strs.length;i ++){
            int noMatches = strs.length - 1;
            for (int j = 0;j < strs.length;j ++){
                if (i != j && isSub(strs[i],strs[j])){
                    noMatches --;
                }
                if (noMatches == 0){
                    return strs[i].length();
                }
            }
        }
        return -1;
    }
    public boolean isSub(String s1,String s2){
        int i = 0;
        for (char c : s2.toCharArray()){
            if (i < s1.length() && s1.charAt(i) == c){
                i ++;
            }
        }
        if (i == s1.length()){
            return false;
        }
        return true;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1604363

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值