scala version(derived problem base on this one)


  • 0
    M
    //a: "apple" , b : "pleqwe" . ==>  result: 3 (length of "qwe")
      
    def findLUSlength(a: String, b: String): Int = {
        if (a.equals(b)) return -1
    
        val intersect = a.intersect(b)
        val aBefore = a.substring(0, a.indexOf(intersect)).length
        val aBehind = a.substring(a.indexOf(intersect), a.length).length - intersect.length
        val bBefore = b.substring(0, b.indexOf(intersect)).length
        val bBehind = b.substring(b.indexOf(intersect), b.length).length - intersect.length
    
        Math.max(Math.max(aBefore, aBehind), Math.max(bBefore, bBehind))
      }
    

Log in to reply
 

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