开发者

Count character is consecutively repeated the most times in the given string

开发者 https://www.devze.com 2023-03-14 23:38 出处:网络
Anybody know, how to count the character is consecutively repeated the most times in the given 开发者_C百科string. I need very shortest answer.

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"]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号