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];
    }
};
相关推荐
hh随便起个名16 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
LYFlied19 小时前
【每日算法】LeetCode 17. 电话号码的字母组合
前端·算法·leetcode·面试·职场和发展
一起养小猫21 小时前
LeetCode100天Day1-字符串匹配与Z字形变换
java·leetcode
yaoh.wang21 小时前
力扣(LeetCode) 1: 两数之和 - 解法思路
python·程序人生·算法·leetcode·面试·跳槽·哈希算法
Code Slacker21 小时前
LeetCode Hot100 —— 滑动窗口(面试纯背版)(四)
数据结构·c++·算法·leetcode
yaoh.wang1 天前
力扣(LeetCode) 27: 移除元素 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·双指针
F_D_Z1 天前
最长连续序列(Longest Consecutive Sequence)
数据结构·算法·leetcode
flashlight_hi1 天前
LeetCode 分类刷题:199. 二叉树的右视图
javascript·算法·leetcode
LYFlied1 天前
【每日算法】LeetCode 46. 全排列
前端·算法·leetcode·面试·职场和发展
LYFlied1 天前
【每日算法】131. 分割回文串
前端·数据结构·算法·leetcode·面试·职场和发展