久しぶりにleetcodeを使ってみた。UIが変わってたり間違えたテストケースを提出しないrunのテストケースに追加できるようになってたりして使いやすくなっていた。
問題
解いた
# @param {String} s1 # @param {String} s2 # @return {Bolean} def check_inclusion(s1, s2) chars = s1.chars.group_by(&:itself).transform_values { |num| num.count } targets = s2.chars start = false deleted = [] targets.each_with_index do |target, index| return true if chars.values.sum.zero? if chars.keys.include?(target) && !start chars[target] -= 1 deleted << target start = true elsif !start next elsif start if !chars.keys.include?(target) start = false chars = s1.chars.group_by(&:itself).transform_values { |num| num.count } deleted = [] next end if chars[target].zero? target_index = deleted.index(target) deleted[0..target_index].each do |target_str| chars[target_str] += 1 end deleted = deleted[(target_index + 1)..] deleted << target chars[target] -= 1 next end chars[target] -= 1 deleted << target end end chars.values.sum.zero? end