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];
    }
};
相关推荐
仙俊红1 天前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
_不会dp不改名_1 天前
leetcode_21 合并两个有序链表
算法·leetcode·链表
吃着火锅x唱着歌1 天前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun1 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌1 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
爱编程的化学家2 天前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
吃着火锅x唱着歌2 天前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
愚润求学2 天前
【贪心算法】day10
c++·算法·leetcode·贪心算法
Tisfy2 天前
LeetCode 0966.元音拼写检查器:三个哈希表实现
leetcode·字符串·散列表·题解·哈希表
ゞ 正在缓冲99%…2 天前
leetcode35.搜索插入位置
java·算法·leetcode·二分查找