0%

longest-uncommon-subsequence-i

Longest Uncommon Subsequence I – LeetCode 521

Problem

Description

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 any subsequence 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.

Answer

Original

Code

1
2
3
4
5
6
class Solution {
public:
int findLUSlength(string a, string b) {
return a == b ? -1 : max(a.size(), b.size());
}
};

思路

求最大不同子串,直接取出更大的大小,枚举几个情况就知道了,这是愚人节出的题,服气。时间复杂度$O(1)$,空间复杂度$O(1)$。
耗时$3$ ms,排名$3.2\%$

Better

思路

还没看到更好的思路。