Memoized version of edit-distance algorithm.