gets while gets do $_.chomp! s = $_.size good = "good" problem = "problem" goodcount = [] problemcount = [] (0...s-good.size+1).each{|i| count = 0 (0...good.size).each{|j| if($_[i+j] != good[j]) count += 1 end } goodcount[i]=count } (0...s-problem.size+1).each{|i| count = 0 (0...problem.size).each{|j| if($_[i+j] != problem[j]) count += 1 end } problemcount[i]=count } mintotal = 999999999 (0...s-good.size-problem.size+1).each{|i| (i+good.size...s-problem.size+1).each{|j| total = goodcount[i] + problemcount[j] mintotal = [total, mintotal].min } } puts mintotal end