Challenge: Shortest Common Supersequence

Given two strings, let's write a function to find their shortest common supersequence.

Problem statement

Given two strings, write a function to find the length of their shortest common supersequence. A supersequence is a string that has both input strings as supersequences.


Two strings


The length of the strings shortest common supersequence

Sample input

string s1 = "abcf";
string s2 = "bdcf";

Sample output

5 // The SCS is "abdcf"

Coding exercise

Take a close look and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.