Longest Common Substring

Find the longest contiguous substring that appears in both strings using an iterative approach.

Code

Algorithms
longest = ''
(0...a.length).each do |i|
  (i+1..a.length).each do |j|
    current = a[i...j]
    longest = current if b.include?(current) && current.length > longest.length
  end
end
return longest

Parameters

First string

Second string

Server

More Ruby Snippets