LeetCode最长特殊序列I & II

LeetCode 521 最长特殊序列I

题目

在这里插入图片描述

在这里插入图片描述

方法:脑筋急转弯

字符串的子序列的长度不会超过该字符串的长度。若子序列的长度等于字符串的长度,那么子序列就是该字符串。

  • 若两字符串相同(长度相同且内容相同),那么任一字符串的子序列均会出现在两个字符串中,此时应返回 -1

  • 若两字符串不相同

    • 当两字符串长度不同时,可以选择较长的字符串作为最长特殊序列,显然它不会是较短的字符串的子序列
    • 当两字符串长度相同时(但不是同一字符串),仍然可以选择其中的一个字符串作为最长特殊序列,它不会是另一个字符串的子序列
class Solution {
    public int findLUSlength(String a, String b) {
        if (a.equals(b)) {
            return -1;
        }
        return Math.max(a.length(), b.length());
    }
}
  • 时间复杂度: O ( n ) O(n) O(n)

  • 空间复杂度: O ( 1 ) O(1) O(1)

String类的equals方法:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

LeetCode 522 最长特殊序列II

题目

在这里插入图片描述

方法一:暴力

生成所有字符串的所有子序列,将其存储在 HashMap 中,并记录每个子序列出现的次数。然后找出出现次数为 1 的最长子序列。如果不存在这样的子序列,返回−1

class Solution {
    public int findLUSlength(String[] strs) {
        HashMap< String, Integer > map = new HashMap < > ();
        for (String s: strs) {
            for (int i = 0; i < (1 << s.length()); i++) {
                String t = "";
                for (int j = 0; j < s.length(); j++) {
//                    System.out.println("i = " + i + " j = " + j + " (i >> j) = " + (i >> j) + " ((i >> j) & 1) = " + ((i >> j) & 1));
                    if (((i >> j) & 1) != 0) {
                        t += s.charAt(j);
                    }
                }
                map.put(t, map.getOrDefault(t, 0) +1);
            }
        }
        int res = -1;
        for (String s: map.keySet()) {
            if (map.get(s) == 1) {
                res = Math.max(res, s.length());
            }
        }
        return res;
    }
}
  • 时间复杂度: O ( n × 2 x ) O(n \times 2^x) O(n×2x),其中 x x x是所有字符串的平均长度, n n n是字符串的数量
  • 空间复杂度: O ( n × 2 x ) O(n \times 2^x) O(n×2x)

方法二:检查每个字符串

如果存在这样的特殊序列,那么它一定是某个给定的字符串。

检查每个字符串是否是其他字符串的子序列:

  • 如果不是,则它是一个特殊序列。最后返回长度最大的特殊序列。
  • 如果不存在特殊序列,返回 -1
class Solution {
    // 判断x是否是y的子序列
    // s 的子序列可以通过删去字符串 s 中的某些字符实现
    public boolean isSubsequence(String x, String y) {
        int j = 0;
        for (int i = 0; i < y.length() && j < x.length(); i++) {
            if (x.charAt(j) == y.charAt(i)) {
                j++;
            }
        }
        return j == x.length();
    }
    public int findLUSlength(String[] strs) {
        int res = -1;
        for (int i = 0; i < strs.length; i++) {
            int j = 0;
            for (j = 0; j < strs.length; j++) {
                if (j == i) {
                    continue ;
                }
                if (isSubsequence(strs[i], strs[j])) {
                    break;
                }
            }
            if (j == strs.length) {
                res = Math.max(res, strs[i].length());
            }
        }
        return res;
    }
}
  • 时间复杂度: O ( x × n 2 ) O(x \times n^2) O(x×n2),其中 n n n是字符串的数量, x x x是每个字符串的平均长度
  • 空间复杂度: O ( 1 ) O(1) O(1)

Reference

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xylitolz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值