LeetCode OJ - Interleaving String
If you want to use (copy, paste or quote) my original article, please contact me through email. (autek.roy@gmail.com) If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
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]; | |
} | |
}; |
If you want to use (copy, paste or quote) my original article, please contact me through email. (autek.roy@gmail.com) If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!