FastPrepInterleaving Strings

Interleaving Strings

CARS24 logoCARS24● HardFULLTIMEONSITE INTERVIEW

Problem statement

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

Use every character from each input exactly once and preserve its order within that input. You may take any number of consecutive characters from either input; strict alternation is not required.

Function

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

Examples

Example 1

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

One valid choice sequence takes characters in their original order from both sources. A valid source-choice witness is 1121212221 (1 means s1 and 2 means s2).

Example 2

s1 = "ab"s2 = "cd"s3 = "adbc"return = false

The d appears before c in the target, contradicting the order of the second input.

Constraints

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

More CARS24 problems

See CARS24 hiring insights
Saved
public boolean isInterleave(String s1, String s2, String s3) {
    // Write your code here.
}
Checking account…