521. Longest Uncommon Subsequence I

Approach 1: String lengths

If string lengths are unequal we can just return the length of the longer string, since it obviously won't be the substring of the smaller string

Moreover if the string lengths are equal and the strings aren't equal, then either of those lengths will work. Moreover as the question states if the strings are equal then we have to return -1

class Solution(object):
    def findLUSlength(self, a, b):
        """
        https://leetcode.com/problems/longest-uncommon-subsequence-i/description/
        :type a: str
        :type b: str
        :rtype: int
        """
        lea, leb = len(a), len(b)
        if lea != leb:
            return max(lea, leb)
        if a == b:
            return -1
        else:
            return lea

Time Complexity: O(N), [length and string comparison are O(N)]

Space Complexity: O(1)

Last updated