Created
October 30, 2014 07:32
-
-
Save autekroy/bd2cadacfb34b96ede2f to your computer and use it in GitHub Desktop.
LeetCode OJ - Interleaving String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isInterleave(string s1, string s2, string s3) { | |
int len1 = s1.size(), len2 = s2.size(), len3 = s3.size(); | |
if(len1 + len2 != len3) | |
return false; | |
bool dp[len1 + 1][len2 + 1]; | |
dp[0][0] = true; | |
for(int i = 1; i <= len1; i++) | |
dp[i][0] = (s1[i - 1] == s3[i - 1])? true: false; | |
for(int i = 1; i <= len2; i++) | |
dp[0][i] = (s2[i - 1] == s3[i - 1])? true: false; | |
for(int i = 1; i <= len1; i++) | |
for(int j = 1; j <= len2; j++){ | |
dp[i][j] = ((s1[i - 1] == s3[i + j - 1]) && dp[i - 1][j] ) | |
|| ((s2[j - 1] == s3[i + j - 1]) && dp[i][j - 1] ); | |
} | |
return dp[len1][len2]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment