2-D Dynamic Programming Two strings
563. Interleaving String
You are given three strings, s1, s2 and s3. Return true if s3 is an interleaving of s1 and s2, and false otherwise.
s3 is an interleaving of s1 and s2 when we can build it one letter at a time, each time taking the next unused letter of s1 or the next unused letter of s2, until both strings are used up. So s3 uses every letter of both strings exactly once, and the letters of each string stay in their order. We may take as many letters in a row from one string as we like.
Any of the three strings can be empty.
Example 1
Input: s1 = "bat", s2 = "ball", s3 = "babtall"
trueExplanation: Take b and a from s1, then b from s2, then t from s1, and last a, l and l from s2.
Example 2
Input: s1 = "", s2 = "sun", s3 = "sun"
trueExplanation: With s1 empty, s3 has to be s2 itself, and it is.
Example 3
Input: s1 = "abc", s2 = "xyz", s3 = "axbzcy"
falseExplanation: Every letter is different, so each letter of s3 can come from only one string. s3 has z before y, but
in s2 the y comes first.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
0 ≤ s1.length, s2.length ≤ 30000 ≤ s3.length ≤ 6000s1,s2ands3hold only lowercase English letters.
Clarifying questions
What you would ask an interviewer before writing any code.