RUBY - Can't pass last test quick enough, bug?


  • 0
    H

    I don't think I can optimize what I have further, should be O(n) - initial case runs in ~62ms

    # @param {String} s
    # @return {Integer}
    def length_of_longest_substring(s)
        counts = {}
        curmax = 0
    
        if s.length == 0
            return curmax
        end
    
        j = offset = 0
        while j + offset < s.length do
            if !counts.has_key?( s[ j + offset ] )
                counts[ s[j + offset] ] = 1
                offset += 1
                if curmax < counts.keys.count
                    curmax = counts.keys.count
                end
            else
                j += 1
                offset = 0
                counts = {}
            end
        end
        return curmax
    end
    

Log in to reply
 

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