Skip to content
BytePatterns

Interleave Two Strings

MediumDynamic Programming#two-string-dp#rolling-row~30m

Problem

A log merger combines two event streams into one without reordering either stream. Given strings a, b and c, return True if c can be formed by interleaving a and b: every character of a and of b is used exactly once, and the characters of a appear in c in their original order, as do the characters of b. a and b have up to 100 characters each, and c up to 200.

Examples

Input:  a = "aabcc", b = "dbbca", c = "aadbbcbcac"
Output: True
Why:    aa from a, dbbc from b, bc from a, a from b, c from a
Input:  a = "aabcc", b = "dbbca", c = "aadbbbaccc"
Output: False
Input:  a = "", b = "", c = ""
Output: True
Why:    edge case, two empty streams merge into an empty one

Hints

0 / 3

Stuck on the idea rather than the code? Edit Distance covers it.