Loading problem...
You are given three strings: strand1, strand2, and fabric. Your task is to determine whether the fabric string can be formed by weaving together strand1 and strand2.
A valid weaving of two strings creates a new string where:
Formally, if strand1 = p₁ + p₂ + ... + pₙ and strand2 = q₁ + q₂ + ... + qₘ (where + denotes concatenation), then a valid weaving could be:
Return true if fabric is a valid weaving of strand1 and strand2, or false otherwise.
Note: The character-by-character order from each source string must be maintained, but individual characters from one string can appear between any characters of the other string as long as the segment alternation constraint is satisfied.
strand1 = "aabcc"
strand2 = "dbbca"
fabric = "aadbbcbcac"trueOne way to obtain fabric is: Split strand1 into "aa" + "bc" + "c", and strand2 into "dbbc" + "a". Weaving the segments alternately: "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac". Since fabric can be obtained by weaving strand1 and strand2, we return true.
strand1 = "aabcc"
strand2 = "dbbca"
fabric = "aadbbbaccc"falseIt is impossible to weave strand2 with any arrangement of strand1 to obtain "aadbbbaccc". The character ordering constraints cannot be satisfied.
strand1 = ""
strand2 = ""
fabric = ""trueBoth source strings and the target fabric are empty, which trivially satisfies the weaving condition.
Constraints