交错字符串
作者:互联网
题源:LeetCode
链接:https://leetcode-cn.com/problems/interleaving-string/
这道题目可以考虑使用动态规划去完成
我们可以找到他的状态转移方程:
其中这个p为i+j-1。
代码实现如下:
1 class Solution { 2 public: 3 bool isInterleave(string s1, string s2, string s3) { 4 auto f = vector < vector <int> > (s1.size() + 1, vector <int> (s2.size() + 1, false)); 5 6 int n = s1.size(), m = s2.size(), t = s3.size(); 7 8 if (n + m != t) { 9 return false; 10 } 11 12 f[0][0] = true; 13 for (int i = 0; i <= n; ++i) { 14 for (int j = 0; j <= m; ++j) { 15 int p = i + j - 1; 16 if (i > 0) { 17 f[i][j] |= (f[i - 1][j] && s1[i - 1] == s3[p]); 18 } 19 if (j > 0) { 20 f[i][j] |= (f[i][j - 1] && s2[j - 1] == s3[p]); 21 } 22 } 23 } 24 25 return f[n][m]; 26 } 27 };
标签:string,s3,s2,s1,vector,交错,字符串,size 来源: https://www.cnblogs.com/hosheazhang/p/15129041.html