Scala solution


  • 0
    V
      def lengthOfLongestSubstring(s: String): Int = {
        val indices = mutable.Map[Char, Int]()
        var left = 0
        var longest = 0
        for ((char, right) <- s.zipWithIndex) {
          indices get char match {
            case None => longest = longest max (right - left + 1)
            case Some(i) => left = left max (i + 1)
          }
          indices.put(char, right)
        }
        longest
      }
    

Log in to reply
 

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