Created
October 11, 2011 21:10
-
-
Save dblack/1279458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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