/97. Interleaving String

97. Interleaving String

Medium
Strings43.6% acceptance

Given three strings str_a, str_b, and target_str, determine if target_str can be formed by interleaving str_a and str_b. An interleaving of two strings is a sequence where the characters of both strings are merged in a way that maintains the order of characters from each string. Return True if target_str can be formed by interleaving str_a and str_b, otherwise return False.

Example 1

Input: str_a=abc, str_b=def, target_str=adbcef

Output: True

Explanation: Characters from str_a and str_b are merged in order to form target_str.

Example 2

Input: str_a=xyz, str_b=uvw, target_str=xuyvwz

Output: False

Explanation: target_str does not preserve the order of characters from str_a and str_b.

Example 3

Input: str_a=', str_b=abc, target_str=abc'

Output: True

Explanation: target_str is identical to str_b.

Constraints

  • 0 <= len(str_a) <= 100
  • 0 <= len(str_b) <= 100
  • 0 <= len(target_str) <= 200
  • str_a, str_b, and target_str consist of lowercase English letters
Python (current runtime)

Case 1

Input: str_a='aa', str_b='bb', target_str='abab'

Expected: True

Case 2

Input: str_a='abc', str_b='xyz', target_str='axbycz'

Expected: True

Case 3

Input: str_a='abc', str_b='xyz', target_str='abxcyz'

Expected: False

Case 4

Input: str_a='', str_b='', target_str=''

Expected: True

Case 5

Input: str_a='a', str_b='b', target_str='ba'

Expected: True