Leetcode 97. 인터레이스 문자열

3992 단어
class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s1.size() + s2.size() != s3.size()) return false;
        vector<bool> dp(s2.size() + 1, false);
        dp[0] = true;
        for (int j = 1; j <= s2.size(); ++j)
            dp[j] = (dp[j - 1] && s2[j - 1] == s3[j - 1]);
        for (int i = 1; i <= s1.size(); ++i) {
            dp[0] = dp[0] & (s1[i - 1] == s3[i - 1]);
            for (int j = 1; j <= s2.size(); ++j)
                dp[j] = (dp[j] && s1[i - 1] == s3[i + j - 1]) |
                (dp[j - 1] && s2[j - 1] == s3[i + j - 1]);
        }
        return dp[s2.size()];
    }
};

압축 행렬
class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s1.size() + s2.size() != s3.size()) return false;
        vector<bool> dp(s2.size() + 1, false);
        dp[0] = true;
        for (int j = 1; j <= s2.size(); ++j)
            dp[j] = (dp[j - 1] && s2[j - 1] == s3[j - 1]);
        for (int i = 1; i <= s1.size(); ++i) {
            dp[0] = dp[0] & (s1[i - 1] == s3[i - 1]);
            for (int j = 1; j <= s2.size(); ++j)
                dp[j] = (dp[j] && s1[i - 1] == s3[i + j - 1]) |
                (dp[j - 1] && s2[j - 1] == s3[i + j - 1]);
        }
        return dp[s2.size()];
    }
};

좋은 웹페이지 즐겨찾기