Problem · String

Interleaving String

Learn this problem
MediumTokopedia logoTokopediaFULLTIMEPHONE SCREEN

Problem statement

Given strings s1, s2, and s3, return whether s3 can be formed by interleaving s1 and s2.

An interleaving uses every character from both source strings exactly once while preserving the left-to-right order within each source. Characters chosen from the two sources may alternate in groups of any positive length.

Function

isInterleave(s1: String, s2: String, s3: String) → boolean

Examples

Example 1

s1 = "aabcc"s2 = "dbbca"s3 = "aadbbcbcac"return = true

The target can choose characters from both sources while retaining each source's internal order.

Example 2

s1 = "aabcc"s2 = "dbbca"s3 = "aadbbbaccc"return = false

No sequence of source choices can produce the target without reversing or skipping a source character.

Example 3

s1 = ""s2 = ""s3 = ""return = true

The empty target is the interleaving of two empty strings.

Constraints

  • 0 <= s1.length, s2.length <= 100.
  • 0 <= s3.length <= 200.
  • All strings contain only lowercase English letters.

More Tokopedia problems

drafts saved locally
public boolean isInterleave(String s1, String s2, String s3) {
    // Write your code here.
}
s1"aabcc"
s2"dbbca"
s3"aadbbcbcac"
expectedtrue
checking account