Skip to content

Commit

Permalink
Create 97. Interleaving String
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeetskd authored Aug 25, 2023
1 parent 61de706 commit 4b0224a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions August/97. Interleaving String
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if(s1.length()+s2.length()!=s3.length()){
return false;
}
int dp[][] = new int[s1.length()+1][s2.length()+1];
for(int i = 0; i<=s1.length(); i++){
Arrays.fill(dp[i], -1);
}
return solve(s1, s2, s3, 0, 0, dp);
}
public boolean solve(String s1, String s2, String s3, int ind1, int ind2, int[][] dp){
if(ind1 + ind2 == s3.length()) return true;
if(dp[ind1][ind2] != -1)
return dp[ind1][ind2] == 1;
boolean ans = false;

if(ind1 < s1.length() && s1.charAt(ind1) == s3.charAt(ind1 + ind2)){
ans |= solve(s1, s2, s3, ind1 + 1, ind2, dp);
}
if(ind2 < s2.length() && s2.charAt(ind2) == s3.charAt(ind1 + ind2)){
ans |= solve(s1, s2, s3, ind1, ind2 + 1, dp);
}
dp[ind1][ind2] = ans ? 1 : 0;
return ans;
}
}

0 comments on commit 4b0224a

Please sign in to comment.