Given S1, S2, S3, find whether S3 is formed by the interleaving of S1 and S2.
For example,
Given:
S1 ="aabcc",
S2 ="dbbca",
When S3 ="aadbbcbcac", Return true.
When S3 ="aadbbbaccc", Return false.
// Solve the problem by using DP. The DP [I] [J] State is represented as S1 [0... i] + S2 [0... if the string range of J] can constitute S3. //, the dynamic transfer equation is: // 1) s1 [I-1] = S3 [I + J-1] & DP [I-1] [J] = true then, DP [I] [J] = true; // 2) s2 [J-1] = S3 [I + J-1] & DP [I] [J-1] = true then DP [I] [J] = true; Class solution {public: bool isinterleave (STD: String S1, STD: String S2, STD: String S3) {If (s1.size () + s2.size ()! = S3.size () return false; STD: vector <bool> dp (s1.size () + 1, STD: vector <bool> (s2.size () + 1, 0); DP [0] [0] = 1; for (INT I = 1; I <s1.size () + 1; I ++) {If (S1 [I-1] = S3 [I-1] & DP [I-1] [0]) DP [I] [0] = true ;} for (INT I = 1; I <s2.size () + 1; I ++) {If (s2 [I-1] = S3 [I-1] & DP [0] [I-1]) DP [0] [I] = true ;} for (INT I = 1; I <s1.size () + 1; I ++) {for (Int J = 1; j <s2.size () + 1; j ++) {If (S1 [I-1] = S3 [I + J-1] & DP [I-1] [J]) DP [I] [J] = true; if (s2 [J-1] = S3 [I + J-1] & DP [I] [J-1]) DP [I] [J] = true ;}} return DP [s1.size ()] [s2.size ()] ;}};
Leetcode-interleaving string