Anybody know, how to count the character is consecutively repeated the most times in the given 开发者_C百科string. I need very shortest answer.
For ex: "xxyyydduuummm" will return 'y, u, m'
"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.sort_by(&:first).last.last.map(&:last) (Ruby 1.9)
"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.sort_by{|k, v| k}.last.last.map{|s, c| c} (Ruby 1.8.7)
# => ["y", "u", "m"]
Improvement Suggested by Mladen Jablanović.
"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.max.last.map(&:last) (Ruby 1.9)
"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.max.last.map{|s, c| c} (Ruby 1.8.7)
# => ["y", "u", "m"]
精彩评论