leetCode.97. 交错字符串

leetCode.97. 交错字符串


题目思路


代码

cpp 复制代码
class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        int n = s1.size(), m = s2.size();
        if ( s3.size() != n + m ) return false;

        vector<vector<bool>> f( n + 1, vector<bool> (m + 1));
        s1 = ' ' + s1;
        s2 = ' ' + s2;
        s3 = ' ' + s3;
        for ( int i = 0; i <= n; ++ i) {
            for (int j = 0; j <= m; ++j ) {
                if ( !i && !j ) f[i][j] = true;
                else {
                    if ( i && s1[i] == s3[i + j]) f[i][j] = f[i - 1][j];
                    if ( j && s2[j] == s3[i + j]) f[i][j] = f[i][j] || f[i][j - 1];
                }
            }
        }

        return f[n][m];
    }
};
相关推荐
Demons_kirit8 小时前
Leetcode 2845 题解
算法·leetcode·职场和发展
Wendy_robot10 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
程序员-King.10 小时前
day49—双指针+贪心—验证回文串(LeetCode-680)
算法·leetcode·贪心算法·双指针
Y1nhl12 小时前
力扣hot100_链表(3)_python版本
python·算法·leetcode·链表·职场和发展
前端 贾公子13 小时前
详解 LeetCode 第 242 题 - 有效的字母组
算法·leetcode·职场和发展
Demons_kirit15 小时前
LeetCode 2799、2840题解
算法·leetcode·职场和发展
软行15 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
雾月5517 小时前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
OpenC++17 小时前
【C++QT】Buttons 按钮控件详解
c++·经验分享·qt·leetcode·microsoft
এ᭄画画的北北18 小时前
力扣-160.相交链表
算法·leetcode·链表