Interleaving String
Check if a third string weaves two strings together
DSA, Medium, 2-D DP
Which structure or algorithm, and what does it cost.
You get three strings, a, b and c. Decide if c is a weave of a and b. A weave uses every letter of a and every letter of b exactly once. It keeps the order inside each string, but letters of the two strings may alternate in any pattern. Return true if c is a weave and false otherwise.
Examples
Constraints
- 0 <= length of
a,b<= 200. - 0 <= length of
c<= 400. - All strings use lowercase English letters.
Your answer must contain
- The simple method and why it is too slow; the better method; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n * m) time and O(m) memory, where n and m are the lengths of a and b.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
If c is a weave, its first letter came from the front of a or from the front of b. What is left to check after that?
Hint 2 the approach
The state is how many letters of a and of b are used so far. Those two counts also tell you which letter of c comes next.
Hint 3 the key step
Make a true or false table by those two counts. A cell is true if a neighbour before it is true and the letter of c matches.
Walkthrough
Full walkthrough brute force to best
Brute force
Choose which places of c belong to a: C(n + m, n) choices, and test each. For two strings of 200 letters that count is about 10^119, so it is out of reach.
Better method
Recursion with memory. Let ok(i, j) ask whether the first i letters of a and the first j letters of b weave into the first i + j letters of c. It is true if the last letter of c came from a (a[i - 1] matches and ok(i - 1, j)) or from b (b[j - 1] matches and ok(i, j - 1)). Store each pair. There are n * m pairs, so O(n * m) time and memory. The recursion can go n + m deep.
Best method
First check that the lengths add up: if len(a) + len(b) is not len(c), the answer is false. Then fill a table of true and false. Cell (i, j) says whether the first i letters of a and the first j letters of b weave into the first i + j letters of c. Cell (0, 0) is true. A cell is true if the cell above is true and a[i - 1] equals the letter c[i + j - 1] they must end on, or if the cell to the left is true and b[j - 1] equals that letter. This is correct because the last letter of any weave came from the end of a or from the end of b, so these two cases cover every weave. A row needs only the row above, so one list updated in place is enough. The answer is the last cell.
Hand trace
Run on a = "moo", b = "cow", c = "mcoowo". The lengths add up (3 + 3 = 6). Each row is one value of i, with columns j = 0 to 3, T for true and F for false. i = 0: T F F F. i = 1: T T T F. i = 2: F T T T. i = 3: F T F T. The last value is T, so the answer is true.
Code
def is_weave(a, b, c):
if len(a) + len(b) != len(c):
return False
ok = [False] * (len(b) + 1) # ok[j]: a[:i] and b[:j] weave into c[:i + j], for the current i
for i in range(len(a) + 1):
for j in range(len(b) + 1):
if i == 0 and j == 0:
ok[j] = True
else:
from_a = i > 0 and ok[j] and a[i - 1] == c[i + j - 1]
from_b = j > 0 and ok[j - 1] and b[j - 1] == c[i + j - 1]
ok[j] = from_a or from_b
return ok[len(b)]Time and memory
O(n * m) time, one check per cell. O(m) memory for the single list.
Common mistakes
- Skipping the length check. With a = "a", b = "" and c = "ab" the table still ends true, because it never looks at the extra letter.
- Matching greedily. With a = "ab", b = "ac" and c = "acab", taking the first letter from a fails, but taking it from b works.
- Reading the wrong letter of c. The letter that cell (i, j) must match is c[i + j - 1], not c[i - 1] or c[j - 1].