Leetcode: Longest Uncommon Subsequence I
Given two strings a
and b
, return the length of the longest uncommon subsequence between a
and b
. A subsequence of a string s
is a string that can be obtained after deleting any number of characters from s
. For example, “abc” is a subsequence of “aebdc” but “aed” is not.
An uncommon subsequence between a
and b
is a string that is a subsequence of one but not the other.
a
and b
can be at most 100.a
and b
are not the same, the longest uncommon subsequence is the longer string because it cannot be a subsequence of the other string. If they are the same, there is no uncommon subsequence, so the result is -1
.a
is equal to b
.-1
.a
and b
.def findLUSlength(a: str, b: str) -> int:
if a == b:
return -1
else:
return max(len(a), len(b))
# Example test cases
print(findLUSlength("aba", "cdc")) # Output: 3
print(findLUSlength("aaa", "bbb")) # Output: 3
print(findLUSlength("aaa", "aaa")) # Output: -1
The time complexity of this solution is O(1) because:
len(a)
and len(b)
are also constant time operations.So, the overall time complexity is constant, O(1).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?