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 beanysubsequence of the other strings.

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

这道题听起来比较玄乎,其实很简单,如果两个String是相同的,那就不存在uncommon subsequence,return -1. 如果两个字符串不一样的话,较长的字符串就是uncommon subsequence,不可能是较短字符串的subsequence

class Solution {
    public int findLUSlength(String a, String b) {
        if(a.equals(b))
            return -1;
        return Math.max(a.length(), b.length());
    }
}

results matching ""

    No results matching ""