zoukankan      html  css  js  c++  java
  • 8.交叉字符串

     题目:给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。

    class Solution {
    public:
        /**
         * Determine whether s3 is formed by interleaving of s1 and s2.
         * @param s1, s2, s3: As description.
         * @return: true of false.
         */
        bool isInterleave(string s1, string s2, string s3) {
            // write your code here
          if (s3.length() != s1.length()+s2.length())
                return false;
            if (s1.length() == 0)
                return s2 == s3;
            if (s2.length() == 0)
                return s1 == s3;
            vector<vector<bool> > dp(s1.length()+1, vector<bool>(s2.length()+1, false));
            dp[0][0] = true;
            for (int i = 1; i <= s1.length(); i++)
                dp[i][0] = dp[i-1][0]&&(s3[i-1] == s1[i-1]);
            for (int i = 1; i <= s2.length(); i++)
                dp[0][i] = dp[0][i-1]&&(s3[i-1] == s2[i-1]);
            for (int i = 1; i <= s1.length(); i++) {
                for (int j = 1; j <= s2.length(); j++) {
                    int t = i+j;
                    if (s1[i-1] == s3[t-1])
                        dp[i][j] = dp[i][j]||dp[i-1][j];
                    if (s2[j-1] == s3[t-1])
                        dp[i][j] = dp[i][j]||dp[i][j-1];
                }
            }
            return dp[s1.length()][s2.length()];
        }
    };

  • 相关阅读:
    linux-01-04(创建文件夹mkdir,进入目录命令cd,创建文件命令 echo cp vim touch等,批量创建文件操作)
    linux-05(tar命令的使用)
    linux-06(移动命令mv)
    linux-07(复制命令cp)
    linux-08(查看命令历史记录history)
    cookie
    vue-router路由懒加载
    setTimeout async promise执行顺序总结
    forEach陷阱
    函数节流与函数防抖之间的区别
  • 原文地址:https://www.cnblogs.com/ALIMAI2002/p/7211138.html
Copyright © 2011-2022 走看看