题目描述:
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be anysubsequence 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 two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
思路:class Solution {
public int findLUSlength(String a, String b) {
if ((a == null && b == null) || (a.length() == 0 && b.length() == 0))
return -1;
if (a == null || a.length() == 0)
return b.length();
if (b == null || b.length() == 0)
return a.length();
if (a.equals(b))
return -1;
return a.length() >= b.length() ? a.length() : b.length();
}
}
本文介绍了一种解决两个字符串间最长不同子序列问题的方法。通过判断字符串是否相等及长度对比,高效得出最长不同子序列的长度。
343

被折叠的 条评论
为什么被折叠?



