FastPrepInterleaving String

Interleaving String

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

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 source strings while retaining each source's internal order.

Example 2

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

Every possible choice eventually needs to reverse or skip a character from one source, so the target is not an interleaving.

Example 3

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

The empty target uses every character from both empty source strings.

Constraints

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

More Bloomberg LP problems

See Bloomberg LP hiring insights
public boolean isInterleave(String s1, String s2, String s3) {
    // Write your code here.
}
s1"aabcc"
s2"dbbca"
s3"aadbbcbcac"
expectedtrue
Checking account…