Advertisement
Singasking

Untitled

Jul 19th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. int sol(string &A, string &B, string &C, int ap, int bp, int cp,vector<vector<vector<int>>>&dp) {
  2. if (ap == A.length() && bp == B.length() && cp == C.length()) return true;
  3. if (ap == A.length() && bp == B.length() && cp != C.length()) return false;
  4. if (cp == C.length() && (ap != A.length() || bp != B.length())) return false;
  5. if(dp[ap][bp][cp] != -1) return dp[ap][bp][cp];
  6. if (ap == A.length()) {
  7. if (B[bp] != C[cp]) return false;
  8. return dp[ap][bp][cp]= sol(A, B, C, ap, bp + 1, cp + 1,dp);
  9. }
  10. if (bp == B.length()) {
  11. if (A[ap] != C[cp]) return false;
  12. return dp[ap][bp][cp]= sol(A, B, C, ap + 1, bp, cp + 1,dp);
  13. }
  14.  
  15. if (A[ap] == C[cp] && B[bp] == C[cp]) {
  16. return dp[ap][bp][cp]=sol(A, B, C, ap, bp + 1, cp + 1,dp) || sol(A, B, C, ap + 1, bp, cp + 1,dp);
  17. } else if (A[ap] == C[cp] && B[bp] != C[cp]) {
  18. return dp[ap][bp][cp]= sol(A, B, C, ap + 1, bp, cp + 1,dp);
  19. } else if (A[ap] != C[cp] && B[bp] == C[cp]) {
  20. return dp[ap][bp][cp]=sol(A, B, C, ap, bp + 1, cp + 1,dp);
  21. } else if (A[ap] != C[cp] && B[bp] != C[cp]) {
  22. return dp[ap][bp][cp]=sol(A,B,C,ap,bp+1,cp,dp) || sol(A,B,C,ap+1,bp,cp,dp);
  23. }
  24.  
  25. return dp[ap][bp][cp]=false;
  26. }
  27.  
  28. int Solution::isInterleave(string A, string B, string C) {
  29.  
  30. vector<vector<vector<int>>> dp(A.size()+2,vector<vector<int>>(B.size()+2,vector<int>(C.size()+2,-1)));
  31. return sol(A,B,C,0,0,0,dp);
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement