Python, Simple Explanation

  • 5

    For strings A, B, when len(A) > len(B), the longest possible subsequence of either A or B is A, and no subsequence of B can be equal to A. Answer: len(A).

    When len(A) == len(B), the only subsequence of B equal to A is B; so as long as A != B, the answer remains len(A).

    When A == B, any subsequence of A can be found in B and vice versa, so the answer is -1.

    def findLUSlength(self, A, B):
        if A == B:
            return -1
        return max(len(A), len(B))

  • 1

    (Edited) Make it 1 line.

    def findLUSlength(self, a, b):
        return -1 if a==b else max(len(a), len(b))

  • 1

    @lee215 Little bug, gets Line 3: NameError: global name 'A' is not defined. (fixed now)

    And just some silly golfing:

        return-1if a==b else max(len(a),len(b))
        return a!=b and max(len(a),len(b))or-1

  • 0

    @StefanPochmann Thx, I make the same little bug everytime.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.