Skip to main content
Back to problems
Leetcode
Medium
Strings
Dynamic Programming
DP on Strings
Google
Interleaving String

Determine whether one string can be formed by interleaving two other strings while preserving the order of characters from each string.

Acceptance 0%
Problem Statement

Interleaving String

You are given three strings s1, s2, and s3. Determine whether s3 can be formed by interleaving s1 and s2.

An interleaving keeps the relative order of characters from each source string. You may take characters from either s1 or s2 one at a time, but you cannot reorder characters within a string.

Return true if s3 can be built this way, otherwise return false.

Notes

  • Every character of s1 and s2 must be used exactly once.
  • At each step, the next character of s3 must match the next unused character from either s1 or s2.
  • This is a classic feasibility problem that is often solved with dynamic programming.

Input Format

  • Three strings: s1, s2, and s3.
  • s3 is the candidate merged string.

Output Format

  • Return a boolean value:
    • true if s3 is an interleaving of s1 and s2
    • false otherwise

Constraints

  • s1, s2, and s3 contain lowercase or arbitrary ASCII characters depending on the platform variant.
  • The length of s3 must equal len(s1) + len(s2) for an interleaving to be possible.
  • Solve efficiently for lengths commonly used in interview-style problems.
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

true

Explanation

One valid merge is a a d b b c b c a c, preserving the order of characters from both s1 and s2.

Example 2

Input

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

Output

false

Explanation

The third string cannot be formed while preserving the relative order of characters from both source strings.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.