#521

Longest Uncommon Subsequence I

pupil · 415 · lc easy +25 · verified · 62.1% accepted · 117 likes · top 63%

play →

Description

Given two strings a and b, find the length of their longest uncommon subsequence — a string that is a subsequence of exactly one of them (not both). If no such string exists, return -1.

Example 1:

Input: a = "aba", b = "cdc"
Output: 3
Explanation: One longest uncommon subsequence is "aba" because "aba" is a subsequence of "aba" but not "cdc".
Note that "cdc" is also a longest uncommon subsequence.

Example 2:

Input: a = "aaa", b = "bbb"
Output: 3
Explanation: The longest uncommon subsequences are "aaa" and "bbb".

Example 3:

Input: a = "aaa", b = "aaa"
Output: -1
Explanation: Every subsequence of string a is also a subsequence of string b. Similarly, every subsequence of string b is also a subsequence of string a. So the answer would be -1.

Code

1
2
3