如何找到重复字母最多的单词 [英] How to find word with the greatest number of repeated letters
问题描述
我的目标是找到给定字符串中重复字母最多的单词.例如,"aabcc ddeeteefef iijjfff"
将返回 "ddeeteefef"
因为 "e"
在这个单词中重复了五次,并且超过了所有其他重复字符.
My goal is to find the word with greatest number of repeated letters in a given string. For example, "aabcc ddeeteefef iijjfff"
would return "ddeeteefef"
because "e"
is repeated five times in this word and that is more than all other repeating characters.
到目前为止,这是我得到的,但它有很多问题并且不完整:
So far this is what I got, but it has many problems and is not complete:
def LetterCountI(str)
s = str.split(" ")
i = 0
result = []
t = s[i].scan(/((.)\2+)/).map(&:max)
u = t.max { |a, b| a.length <=> b.length }
return u.split(//).count
end
我的代码只能找到连续的模式;如果模式被打断(例如使用 "aabaaa"
,它会计数 3 次而不是 5 次).
The code I have only finds consecutive patterns; if the pattern is interrupted (such as with "aabaaa"
, it counts a three times instead of five).
推荐答案
我会这样做:
s = "aabcc ddeeteefef iijjfff"
# intermediate calculation that's happening in the final code
s.split(" ").map { |w| w.chars.max_by { |e| w.count(e) } }
# => ["a", "e", "f"] # getting the max count character from each word
s.split(" ").map { |w| w.count(w.chars.max_by { |e| w.count(e) }) }
# => [2, 5, 3] # getting the max count character's count from each word
# final code
s.split(" ").max_by { |w| w.count(w.chars.max_by { |e| w.count(e) }) }
# => "ddeeteefef"
更新
each_with_object
比 group_by
方法给出更好的结果.
each_with_object
gives better result than group_by
method.
require 'benchmark'
s = "aabcc ddeeteefef iijjfff"
def phrogz(s)
s.scan(/\w+/).max_by{ |word| word.chars.group_by(&:to_s).values.map(&:size).max }
end
def arup_v1(s)
max_string = s.split.max_by do |w|
h = w.chars.each_with_object(Hash.new(0)) do |e,hsh|
hsh[e] += 1
end
h.values.max
end
end
def arup_v2(s)
s.split.max_by { |w| w.count(w.chars.max_by { |e| w.count(e) }) }
end
n = 100_000
Benchmark.bm do |x|
x.report("Phrogz:") { n.times {|i| phrogz s } }
x.report("arup_v2:"){ n.times {|i| arup_v2 s } }
x.report("arup_v1:"){ n.times {|i| arup_v1 s } }
end
输出
user system total real
Phrogz: 1.981000 0.000000 1.981000 ( 1.979198)
arup_v2: 0.874000 0.000000 0.874000 ( 0.878088)
arup_v1: 1.684000 0.000000 1.684000 ( 1.685168)
这篇关于如何找到重复字母最多的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!