AC Ruby Solution


  • 0
    S
    def length_of_longest_substring(s)
      length = s.length
      hash = {}
      max_length = i = j = 0
      while (j < length) do
        if (hash[s[j]] != nil)
          i = [hash[s[j]], i].max
        end
        max_length = [max_length, (j - i + 1)].max
        hash[s[j]] = j+1
        j += 1
      end
      max_length
    end
    

Log in to reply
 

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