Lesson overview · Free interview practice

Longest Common Substring

Start with Find a Longest Common Substring for substring practice. Prefix, subsequence and edit distance are optional comparisons.

Find the longest substring common to two strings.

Topics in the full lesson
  • Concept and Use Cases
  • When to Use
  • Time and Space Complexity
  • Common Problems
  • Longest Common Substring
  • Finding the Longest Common Substring Itself

Practice Problems

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

Completion marks record your own progress, not an automatically checked result. The task type does not determine whether it is optional.

Question

Write a function to find the longest common prefix string amongst an array of strings strs. If there is no common prefix, return an empty string "".

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence appears in the same relative order but is not necessarily contiguous.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

CodingHard

Edit Distance

Question

Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. Allowed operations: insert a character, delete a character, or replace a character.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

CodingMedium

Find a Longest Common Substring

Implement and check your solution in the stated environment before revealing the answer.

Question

Write findLongestCommonSubstring(s1, s2) to return one longest contiguous substring shared by both strings. Explain what your table state means and the time and space your solution uses.

Each input has 0 to 500 characters, inclusive. Inputs contain only printable ASCII characters from U+0020 to U+007E, including spaces. Matching is case-sensitive.

Return "" if either input is empty or the strings share no character. If several longest substrings tie, return any one of them. Inputs outside these constraints do not need special handling.

Starter and local commands

Use Node 24. The TypeScript option also needs TypeScript 5.9.2 available as tsc. You don't need that compiler for the JavaScript starter. This task has no in-site editor, Run button or automatic judge.

Choose one starter and save it as substring.js or substring.ts. It deliberately throws Not implemented: write your solution here. Replace that throw with your implementation before expecting a result.

function findLongestCommonSubstring(s1, s2) {
    throw new Error("Not implemented: write your solution here.");
}

console.log(findLongestCommonSubstring("abcdxyz", "xyzabcd"));

Run these commands from the folder containing your file:

node --version
node substring.js

For TypeScript, check that tsc --version reports 5.9.2. Compilation can succeed while the placeholder still throws. Compile again after editing before running the emitted JavaScript.

The sample should return abcd. Also try abc with ac, where either a or c is valid, and an empty input, which should return "". Explain your table state before comparing it with the answer.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Longest Common Substring material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium