Over a million developers have joined DZone.

Computes A Number From Two Strings

·
def foobar(astring, bstring)
  Hash.new do |l,(i, j)|
    if l.key? [i, j]
      l[[i, j]]
    else
      a, b = astring[i], bstring[j]
      l[[i, j]] = case
      when !a, !b then  0
      when a == b then  1 + l[[i + 1, j + 1]]
      else              [ l[[i + 1, j]], l[[i, j + 1]] ].max
      end
    end
  end[[0, 0]]
end
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}