36 · DP Patterns

#97 Interleaving String

Mediumsolve on LeetCode ↗

frequently reported atAmazonMicrosoftGoogle

the problem

In plain words

Decide whether s3 can be built by merging s1 and s2 letter by letter, keeping the letters of s1 in their original order and the letters of s2 in theirs. Every letter of s1 and of s2 must be used exactly once.

in s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"

out true

aa (s1) + dbbc (s2) + bc (s1) + a (s2) + c (s1).

in s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"

out false

in s1 = "", s2 = "", s3 = ""

out true

  • 0 ≤ s1.length, s2.length ≤ 100
  • 0 ≤ s3.length ≤ 200
  • Lowercase English letters
  • Follow-up: O(s2.length) extra memory

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #115
      Distinct Subsequences
      Hardin DP Patterns
    2. #1143
      Longest Common Subsequence
      Mediumin Dynamic Programming
    3. #10
      Regular Expression Matching
      Hardin DP Patterns