Skip to content

Commit

Permalink
Ruby Solution for Minimum Window Substring
Browse files Browse the repository at this point in the history
  • Loading branch information
saip7795 committed Jan 6, 2023
1 parent eff3535 commit 3a5138e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ruby/0076-min-window-substring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

def min_window(s, t)

return "" if t == ""
count_t = Hash.new(0)
window = Hash.new(0)

t.each_char do |c|
count_t[c] +=1
end

have = 0
need = count_t.length

res = [-1,-1]
result_length = 9999999999
l = 0
(0..s.length-1).each do |r|
c = s[r]
window[c] +=1

have +=1 if ((count_t.keys.include?(c)) && (window[c] == count_t[c]))
while(have == need)
if (r-l+1) < result_length
res = [l,r]
result_length = (r-l+1)

end
window[s[l]] -=1
have -=1 if ((count_t.keys.include?(s[l])) && (window[s[l]] < count_t[s[l]]))
l +=1
end
end
l = res[0]
r = res[1]
if result_length != 9999999999
return s[l..r]
else
return ""
end
end

0 comments on commit 3a5138e

Please sign in to comment.