Skip to content

Instantly share code, notes, and snippets.

@dblack
Created October 11, 2011 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblack/1279458 to your computer and use it in GitHub Desktop.
Save dblack/1279458 to your computer and use it in GitHub Desktop.
def max_length_histogram(str)
Hash.new(0).tap do |hist|
str.scan(/((.)\2*)/) do |substring, letter|
hist[letter] = [ hist[letter], substring.size ].max
end
end
end
def longest_run(str)
max_length_histogram(str).max_by {|letter, n| n }
end
str = "aabbbbcccdebbbbbff"
p longest_run(str) # ["b", 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment