My Accepted Scala Solution


  • 0
    K

    def lengthOfLongestSubstring(s: String): Int = {
    if(s == null || s.length==0){
    return 0;
    }
    var last_start = 0
    var cur_len,max_len = 1
    var visit = new scala.collection.mutable.HashMapChar,Int
    visit += (s.charAt(0) -> 0)
    for (i <- 1 until s.length){
    val key = s.charAt(i)
    if(!visit.contains(key)){
    cur_len += 1
    visit += (key -> i )
    }else{
    if(last_start <= visit(key)){
    cur_len = i - visit(key)
    last_start = visit(key)+1
    visit(key) = i
    }else{
    cur_len += 1
    visit(key)=i
    }
    }
    if(max_len<cur_len){
    max_len = cur_len
    }
    }
    max_len
    }


Log in to reply
 

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