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 小时前
力扣hoT100之找到字符串中所有字母异位词(java版)
java·数据结构·算法·leetcode
YoungHong19922 小时前
面试经典150题[063]:删除链表的倒数第 N 个结点(LeetCode 19)
leetcode·链表·面试
青山的青衫2 小时前
【前后缀】Leetcode hot 100
java·算法·leetcode
啊吧怪不啊吧3 小时前
二分查找算法介绍及使用
数据结构·算法·leetcode
Kuo-Teng12 小时前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
橘颂TA16 小时前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++
愚润求学19 小时前
【动态规划】专题完结,题单汇总
算法·leetcode·动态规划
·白小白20 小时前
力扣(LeetCode) ——43.字符串相乘(C++)
c++·leetcode
一匹电信狗1 天前
【C++11】Lambda表达式+新的类功能
服务器·c++·算法·leetcode·小程序·stl·visual studio
在等晚安么1 天前
力扣面试150题打卡
算法·leetcode·面试