Thursday, February 5, 2026
HomeLanguagesJavascriptJavaScript Program to Find Longest Common Substring Between Two Strings

JavaScript Program to Find Longest Common Substring Between Two Strings

In this article, we will see how to find the longest common substring between two strings in JavaScript.

A substring is a contiguous sequence of characters within a string. It can be obtained by extracting part of the string starting from any position. We are going to write a JavaScript function that will take two strings and find the common longest consecutive string.

There are two approaches to finding the longest common substring between two strings

  • Brute-force approach
  • Dynamic programming

Approach 1: Brute-force approach

In this method, we create all possible substrings from both strings and compare them to determine which substring has the longest length in common. The findLongestCommonSubstring() function uses nested loops to generate all possible substrings and compares them to find the longest common substring. In this example, the function findLongestCommonSubstring returns the longest common substring between any two input strings (str1 and str2) given two input strings. In this method time complexity is O(n*m), where n and m are the lengths of the input strings.

Example:

Javascript




function findLongestCommonSubstring(str1, str2) {
    let longestSubstring = "";
  
    for (let i = 0; i < str1.length; i++) {
        for (let j = 0; j < str2.length; j++) {
            let substring = "";
            let x = i;
            let y = j;
  
            while (x < str1.length && 
                   y < str2.length && 
                   str1[x] === str2[y]) {
                substring += str1[x];
                x++;
                y++;
            }
  
            if (substring.length > longestSubstring.length) {
                longestSubstring = substring;
            }
        }
    }
  
    return longestSubstring;
}
  
const string1 = "GeeksForneveropen";
const string2 = "Geekscode";
  
const longestCommonSubstring = 
      findLongestCommonSubstring(string1, string2);
        
console.log("Longest Common Substring:"
    longestCommonSubstring);


Output

Longest Common Substring: Geeks

Approach 2: Dynamic Programming

The dynamic programming is the optimised approach of the previous method. In this method we will use 2D array for storing the lengths of common substrings between str1 and str2. In this example, we are creating an LCS table to store the length of the longest common substring ending at that position. After filling the table by iterating through both strings and incrementing the value if the characters match. Then we keep track of the maximum length seen and the corresponding substring and return the longest substring found.

Example:

Javascript




function longestCommonSubstring(str1, str2) {
    let n = str1.length;
    let m = str2.length;
  
    let lcs = [];
    for (let i = 0; i <= n; i++) {
        lcs[i] = [];
        for (let j = 0; j <= m; j++) {
            lcs[i][j] = 0;
        }
    }
  
    let result = "";
    let max = 0;
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < m; j++) {
            if (str1[i] === str2[j]) {
                lcs[i + 1][j + 1] = lcs[i][j] + 1;
                if (lcs[i + 1][j + 1] > max) {
                    max = lcs[i + 1][j + 1];
                    result = str1.substring(i - max + 1, i + 1);
                }
            }
        }
    }
  
    return result;
}
  
let str1 = "GeeksForneveropen";
let str2 = "Geekscode";
  
let result = longestCommonSubstring(str1, str2);
  
console.log("Longest Common Substring:", result);


Output

Longest Common Substring: Geeks
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32487 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6861 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12071 POSTS0 COMMENTS
Shaida Kate Naidoo
6994 POSTS0 COMMENTS
Ted Musemwa
7233 POSTS0 COMMENTS
Thapelo Manthata
6944 POSTS0 COMMENTS
Umr Jansen
6926 POSTS0 COMMENTS