You are here! Your runtime beats 100.00% of rubysubmissions.


  • 0
     def length_of_longest_substring(s)
             s_array = s.split(//);
        dic = Hash.new;
        max_length = 0;
        start = 0;
        temp_length = 0;
        index = 0;
        s_array.each do |val|
            if dic.has_key?(val) then
                ret = dic[val];
                dic[val] = index;
                
                if ret<start then
                   temp_length += 1;
                else
                   start = ret + 1;
                   temp_length = index - start + 1;
                end
            else
                temp_length += 1;
                dic[val] = index;
            end
            
            if temp_length > max_length then
                max_length = temp_length;
            end
            
            index += 1;
        end
        return max_length;
    end

Log in to reply
 

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