Longest Common Substring

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

Code

Algorithms
let longest = '', current = '';
for (let i = 0; i < a.length; i++) {
  for (let j = i + 1; j <= a.length; j++) {
    current = a.slice(i, j);
    if (b.includes(current) && current.length > longest.length) longest = current;
  }
}
return longest;

Parameters

First string

Second string

Browser·fetch() may be limited by CORS

More JavaScript Snippets