Given two strings a
and b
, return the length of the longest uncommon subsequence between a
and b
. If no such uncommon subsequence exists, return -1
. An uncommon subsequence between two strings is a string that is a subsequence of exactly one of them.
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.
Constraints:
1 <= a.length, b.length <= 100
a
and b
consist of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach means checking every single possibility to find the longest uncommon subsequence. It's like comparing each word against every other word, one by one, to see if one is a special part of the other.
Here's how the algorithm would work step-by-step:
def find_longest_uncommon_subsequence_length(first_string, second_string):
# Check if the strings are identical
if first_string == second_string:
return -1
first_string_length = len(first_string)
second_string_length = len(second_string)
# Determine if one string is a subsequence of the other
if is_subsequence(first_string, second_string):
return second_string_length
# Determine if one string is a subsequence of the other
if is_subsequence(second_string, first_string):
return first_string_length
# If neither is a subsequence of the other, return the length of the longer string
return max(first_string_length, second_string_length)
def is_subsequence(shorter_string, longer_string):
shorter_string_index = 0
longer_string_index = 0
while shorter_string_index < len(shorter_string) and longer_string_index < len(longer_string):
if shorter_string[shorter_string_index] == longer_string[longer_string_index]:
shorter_string_index += 1
longer_string_index += 1
# Check if all characters of shorter_string were found in longer_string
return shorter_string_index == len(shorter_string)
The problem asks for the length of the longest string if it is not a subsequence of the other. The trick is to realize there are only a few possibilities we need to check instead of trying every subsequence combination. We can use some clever comparisons.
Here's how the algorithm would work step-by-step:
def longest_uncommon_subsequence(first_string, second_string):
# If strings are identical, there's no uncommon subsequence.
if first_string == second_string:
return 0
# If one string is longer, it's an uncommon subsequence of the other.
if len(first_string) != len(second_string):
return max(len(first_string), len(second_string))
# If strings have the same length but are different,
# either one is an uncommon subsequence.
return len(first_string)
Case | How to Handle |
---|---|
Both strings are null or empty | Return -1 as no subsequence can be formed. |
One string is null or empty, the other is not | Return the length of the non-null/empty string as it is a subsequence of itself but not of the null/empty string. |
Strings are identical | Return -1 because neither string is a subsequence of the other and therefore no *uncommon* subsequence exists. |
One string is a subsequence of the other | Return the length of the longer string, which is the uncommon subsequence. |
Strings are of different lengths and share a common prefix/suffix | A simple length comparison suffices, as the longer string contains the uncommon subsequence. |
Maximum string length is reached | Ensure the algorithm doesn't lead to integer overflows when calculating lengths and that memory usage is reasonable; length comparison is O(1). |
Strings contain non-ASCII characters | The length function should correctly handle UTF-8 or other encodings without errors. |
Strings contain only whitespace characters | The whitespace will be included in the length calculation and handled the same as other characters. |