Java 1-liner


  • 21
    public int findLUSlength(String a, String b) {
        return a.equals(b) ? -1 : Math.max(a.length(), b.length());
    }
    

  • 4

    I want to know should we consider the situation that a or b is null?


  • 5
    J

    By default, unless explicitly written in specification of the method, null values are not allowed to be passed in arguments.


  • 0
    S

    I can't agree with this method.
    for example,a="abc",b="avbfc",the result should be -1 instead of 5 according to "A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. "


  • 2
    K

    @Scenery_S For the case of a="abc" and b="avbfc", the result should actually be 5. The longest uncommon subsequence would be "avbfc"; it's a subsequence of avbfc, and not of abc, fulfilling the requirement for longest uncommon subsequence.


  • 0
    G

    @compton_scatter oh,we are the same:

    public class Solution {
        public int findLUSlength(String a, String b) {
          return a.equals(b)?-1:Math.max(a.length(),b.length());  
        }
    }
    

  • 0
    S

    @Scenery_S If you read the definition in the question again:

    The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

    which means in your case,

    avbfc is subsequence of avbfc, but not subsequence of abc, thus it's the right answer.


  • 0
    Z

    @雾里看花cjj yeah, I also think should consider the situation that a or b is null, because if a == null, a.length() will throw exception


Log in to reply
 

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