LeetCode刷题实战521: 最长特殊序列 Ⅰ
共 2044字,需浏览 5分钟
·
2022-02-12 06:17
Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If the longest uncommon subsequence does not exist, return -1.
An uncommon subsequence between two strings is a string that is a subsequence of one but not the other.
A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.
For example, "abc" is a subsequence of "aebdc" because you can delete the underlined characters in "aebdc" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).
示例
示例 1:
输入: a = "aba", b = "cdc"
输出: 3
解释: 最长特殊序列可为 "aba" (或 "cdc"),两者均为自身的子序列且不是对方的子序列。
示例 2:
输入:a = "aaa", b = "bbb"
输出:3
解释: 最长特殊序列是“aaa”和“bbb”。
示例 3:
输入:a = "aaa", b = "aaa"
输出:-1
解释: 字符串a的每个子序列也是字符串b的每个子序列。同样,字符串b的每个子序列也是字符串a的子序列。
解题
class Solution {
public int findLUSlength(String a, String b) {
int lenA = a.length();
int lenB = b.length();
if(lenA != lenB)
return Math.max(lenA,lenB);
else{
if(a.equals(b))
return -1;
else
return lenA;
}
}
}
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。