DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Computes A Number From Two Strings

04.24.2005
| 2168 views |
  • submit to reddit
        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