问题描述:
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=b, 则非公共子序列不存在,返回-1
- 若a不等于b:
(1)若ab长度相等,则ab为彼此的最长非公共子序列
(2)若ab长度不等,更长的为二者的最长非公共子序列
代码如下:
class Solution {
public int findLUSlength(String a, String b) {
if(a.equals(b)) return -1;
else return Math.max(a.length(),b.length());
}
}