Practice › Dynamic Programming
Interleaving String
Medium · Dynamic Programming · Asked at Google, Microsoft, Amazon, Apple
Print YES if s3 can be formed by interleaving s1 and s2 — that is, by merging the two strings while keeping the order of the characters of each — otherwise NO.
Input
Three lines: s1, s2 and s3.
Output
YES or NO.
Constraints
- 1 ≤ |s1|, |s2| ≤ 500
- 1 ≤ |s3| ≤ 1000
- only lowercase letters
ok[i][j] = the first i characters of s1 and the first j of s2 can form the first i + j of s3. It comes from ok[i − 1][j] (if s1[i − 1] matches) or ok[i][j − 1] (if s2[j − 1] matches). If the lengths do not add up, the answer is NO.
Example 1
Input
aabcc dbbca aadbbcbcac
Output
YES
Example 2
Input
aabcc dbbca aadbbbaccc
Output
NO
Hints
- If |s1| + |s2| ≠ |s3|, the answer is immediately NO.
- The next character of s3 must come from s1 or from s2.
- ok[i][j]: can the first i of s1 and first j of s2 build the first i + j of s3?
Approach
ok[0][0] = true. ok[i][j] is true if (ok[i − 1][j] and s1[i − 1] = s3[i + j − 1]) or (ok[i][j − 1] and s2[j − 1] = s3[i + j − 1]). Fill the table row by row and answer ok[|s1|][|s2|]. A greedy choice fails when both strings offer the same character, which is why the table is needed. One row is enough if you want O(|s2|) memory.
Complexity: O(|s1| · |s2|) time · O(|s2|) space with one row
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b, c;
cin >> a >> b >> c;
int n = a.size(), m = b.size();
if (n + m != (int) c.size()) {
cout << "NO\n";
return 0;
}
vector<bool> ok(m + 1, false);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++) {
if (i == 0 && j == 0) ok[j] = true;
else {
bool fromA = i > 0 && ok[j] && a[i - 1] == c[i + j - 1];
bool fromB = j > 0 && ok[j - 1] && b[j - 1] == c[i + j - 1];
ok[j] = fromA || fromB;
}
}
cout << (ok[m] ? "YES" : "NO") << "\n";
return 0;
}Python solution
import sys
a, b, c = sys.stdin.read().split()[:3]
n, m = len(a), len(b)
if n + m != len(c):
print("NO")
else:
ok = [False] * (m + 1)
for i in range(n + 1):
for j in range(m + 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
print("YES" if ok[m] else "NO")Java solution
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a = br.readLine().trim(), b = br.readLine().trim(), c = br.readLine().trim();
int n = a.length(), m = b.length();
if (n + m != c.length()) {
System.out.println("NO");
return;
}
boolean[] ok = new boolean[m + 1];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++) {
if (i == 0 && j == 0) ok[j] = true;
else {
boolean fromA = i > 0 && ok[j] && a.charAt(i - 1) == c.charAt(i + j - 1);
boolean fromB = j > 0 && ok[j - 1] && b.charAt(j - 1) == c.charAt(i + j - 1);
ok[j] = fromA || fromB;
}
}
System.out.println(ok[m] ? "YES" : "NO");
}
}