C++ one line Solution

  • 1
    class Solution {
        int findLUSlength(string a, string b) {
            return a == b?-1:max(a.size(), b.size());

  • 0

    Cool, without unnecessary if/else.

  • 1

    Your solution has a room to improve for handling large test cases.
    Comparing the two long strings is quite expensive work, so you will want to avoid it as possible.

    We don't need to do the string comparison if the two strings has different size, which should be the majority of the use cases.

            if (a.size() != b.size())
                 return max(a.size(), b.size());
                 return (a == b)?-1:a.size();

Log in to reply

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