[InterviewBit] Interleaving Strings

Interleaving Strings

  • Time : O(nm)
  • Space : O(nm)
1
2
3
4
5
6
7
8
9
10
11
12
int Solution::isInterleave(string A, string B, string C) {
if(A.length() + B.length() != C.length()) return 0;
int dp[155][155] = {};
dp[0][0] = 1;
for(int i = 0; i <= A.length(); i++) {
for(int j = 0; j <= B.length(); j++) {
if(i != A.length() and C[i+j] == A[i]) dp[i+1][j] |= dp[i][j];
if(j != B.length() and C[i+j] == B[j]) dp[i][j+1] |= dp[i][j];
}
}
return dp[A.length()][B.length()];
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/29/PS/interviewbit/interleaving-strings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.