如何找到重复字母数最多的单词

Ste*_*ens 5 ruby

我的目标是找到给定字符串中具有最多重复字母数的单词.例如,"aabcc ddeeteefef iijjfff"会返回,"ddeeteefef"因为"e"在这个单词中重复了五次,这比所有其他重复的字符多.

到目前为止,这是我得到的,但它有很多问题,并不完整:

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
Run Code Online (Sandbox Code Playgroud)

我只能找到连续模式的代码; 如果模式被中断(例如"aabaaa",它会计数三次而不是五次).

Phr*_*ogz 6

str.scan(/\w+/).max_by{ |w| w.chars.group_by(&:to_s).values.map(&:size).max }
Run Code Online (Sandbox Code Playgroud)
  • scan(/\w+/) - 创建所有"单词"字符序列的数组
  • max_by{ … } - 找到在该块内给出最大值的单词
  • chars - 将字符串拆分为字符
  • group_by(&:to_s) - 创建一个散列映射,将每个字符映射到所有出现的数组
  • values - 只需获取所有出现的数组
  • map(&:size) - 将每个数组转换为该数组中的字符数
  • max- 找到最大的字符并将其作为max_by检查的结果

编辑:写得不那么紧凑:

str.scan(/\w+/).max_by do |word|
  word.chars
      .group_by{ |char| char }
      .map{ |char,array| array.size }
      .max
end
Run Code Online (Sandbox Code Playgroud)

写得少功能,少用Ruby-isms(使它看起来更像"其他"语言):

words_by_most_repeated = []
str.split(" ").each do |word|
  count_by_char = {} # hash mapping character to count of occurrences
  word.chars.each do |char|
    count_by_char[ char ] = 0 unless count_by_char[ char ]
    count_by_char[ char ] += 1
  end
  maximum_count = 0
  count_by_char.each do |char,count|
    if count > maximum_count then
      maximum_count = count
    end
  end
  words_by_most_repeated[ maximum_count ] = word
end

most_repeated = words_by_most_repeated.last
Run Code Online (Sandbox Code Playgroud)


Aru*_*hit 4

我会这样做:

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"
Run Code Online (Sandbox Code Playgroud)

更新

each_with_object给出比group_by方法更好的结果。

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
Run Code Online (Sandbox Code Playgroud)

输出

            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)
Run Code Online (Sandbox Code Playgroud)

  • 请注意,虽然我的答案并不那么有效,但这个答案在算法上特别糟糕。它的解释很清楚,并且对于短字符串有效,但使用 `word.chars{ |char| 随着字符串的增长,word.count(char) }` 会带来 O(n^2) 的性能。 (2认同)