LeetCode "Interleaving String"

I briefly took a look at: http://yucoding.blogspot.com/2013/01/leetcode-question-27-interleaving-string.html And I got 1A :)

This is the best problem to understand DP deeper. At the very first sight to this problem statement, DP pattern may not be that obvious. Yes, DP is hard. But again, you should be able to figure out a DFS solution. So, please revise: what is the repeating computation pattern of the DFS solution? Right, s1[i] & s2[j] may be computed multiple times, when backtrace occurs in your DFS. And here comes the punch line of this problem:

DP pattern should address your enumerationDFS repeating computation pattern! That's exactly the purpose of DP!

Here's the code:

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        size_t len1 = s1.length();
        size_t len2 = s2.length();
        size_t len3 = s3.length();
        if ((len1 + len2) != len3) return false;

        //    Init
        vector<bool> ret; ret.resize(len3 + 1);
        std::fill(ret.begin(), ret.end(), false);

        vector<vector<bool>> dp;    //    dp[i][j]: s1: len i, s2: len j -> valid?
        dp.resize(len2 + 1);
        for (int i = 0; i < len2 + 1; i++)
        {
            dp[i].resize(len1 + 1);
            std::fill(dp[i].begin(), dp[i].end(), false);
        }

        //    Go DP: 
        //        dp[i + 1][j] = dp[i][j] ? (s[i + j] == s1[i]) : false
        //        then, ret[i + j + 1] = dp[i + 1][j]
        for (int i = 0; i <= len1; i ++)
        for (int j = 0; j <= len2; j++)
        {
            if (i == 0 && j == 0)
            {
                dp[i][j] = true;
                ret[0] = true;
                continue;
            }

            if (i == 0)
            {
                dp[j][i] = dp[j - 1][i] && (s3[j - 1] == s2[j - 1]);
            }
            else if (j == 0)
            {
                dp[j][i] = dp[j][i - 1] && (s3[i - 1] == s1[i - 1]);
            }
            else
            {
                bool bi = dp[j - 1][i] && (s3[i + j - 1] == s2[j - 1]);
                bool bj = dp[j][i - 1] && (s3[i + j - 1] == s1[i - 1]);
                dp[j][i] = dp[j][i] | bi | bj;
            }

            ret[i + j] = ret[i + j] | dp[j][i];
        }
        return ret[len3];
    }
};
原文地址:https://www.cnblogs.com/tonix/p/3904425.html