ans = [] w_counts = [] index = 0 w_start_flag = false gets.chomp.split('').each do |e| if e == 'w' w_start_flag = true w_counts[index] ||= 0 w_counts[index] += 1 end if w_start_flag && e != 'w' w_start_flag = false index += 1 end ans[index] ||= '' ans[index] += e end ans.each_with_index do |e, i| e.gsub!(/w/, '') next if e == '' puts e if w_counts[i] == w_counts.max end