master
 1#!/usr/bin/env ruby
 2
 3=begin
 4Given a string, find the length of the longest substring without repeating characters.
 5
 6class Solution:
 7  def lengthOfLongestSubstring(self, s):
 8    # Fill this in.
 9
10print Solution().lengthOfLongestSubstring('abrkaabcdefghijjxxx')
11# 10
12
13Can you find a solution in linear time?
14=end
15
16def assert_equal(x, y)
17  raise [x, y].inspect unless x == y
18end
19
20class Solution
21  def self.length_of_longest_substring(value)
22    last = nil
23    count = 0
24    max = 0
25    value.chars.each_with_index do |char, i|
26      if char == last
27        count = 1
28      else
29        count += 1
30      end
31      max = count if count > max
32      last = char
33    end
34    max
35  end
36end
37
38assert_equal 10, Solution.length_of_longest_substring('abrkaabcdefghijjxxx')
39puts "Yay!"