zoukankan      html  css  js  c++  java
  • LeetCode

    Interleaving String

    2014.2.26 02:48

    Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

    For example,
    Given:
    s1 = "aabcc",
    s2 = "dbbca",

    When s3 = "aadbbcbcac", return true.
    When s3 = "aadbbbaccc", return false.

    Solution1:

      This problem can be solved with DFS, but the time is untolerable. Dynamic programming would be a better way out.

      The word "interleaving" means every letter from s3 must come from either s1 or s2, so every letter in s3 will be compared with s1 and s2.

      Let f[i][j] be the DP array. f[i][j] means whether s1[1:i] and s2[1:j] forms the interleaving string in s3[1:i+j]:

        1. f[0][0]=true.

        2. if s3[i+j]==s1[i] and f[i-1][j]==true, f[i][j]=true.

        3. if s3[i+j]==s2[j] and f[i][j-1]==true, f[i][j]=true.

      Total time and space complexities are both O(n^2).

    Accepted code:

     1 // 3CE, 1TLE, 2WA, 1AC, O(n^2) solution with DP, space can be optimized.
     2 class Solution {
     3 public:
     4     bool isInterleave(string s1, string s2, string s3) {
     5         int len1;
     6         int len2;
     7         int len3;
     8         
     9         len1 = (int)s1.length();
    10         len2 = (int)s2.length();
    11         len3 = (int)s3.length();
    12         if (len3 != len1 + len2) {
    13             return false;
    14         }
    15         
    16         if (len1 == 0) {
    17             return s2 == s3;
    18         } else if (len2 == 0) {
    19             return s1 == s3;
    20         }
    21         
    22         int i, j;
    23         dp.resize(len1 + 1);
    24         for (i = 0; i < len1 + 1; ++i) {
    25             dp[i].resize(len2 + 1);
    26         }
    27         
    28         dp[0][0] = 1;
    29         for (i = 1; i <= len1; ++i) {
    30             if (dp[i - 1][0] && s3[i - 1] == s1[i - 1]) {
    31                 dp[i][0] = 1;
    32             }
    33         }
    34         for (j = 1; j <= len2; ++j) {
    35             if (dp[0][j - 1] && s3[j - 1] == s2[j - 1]) {
    36                 dp[0][j] = 1;
    37             }
    38         }
    39         for (i = 1; i <= len1; ++i) {
    40             for (j = 1; j <= len2; ++j) {
    41                 dp[i][j] = 0;
    42                 dp[i][j] = dp[i][j] || (dp[i - 1][j] && (s3[i + j - 1] == s1[i - 1]));
    43                 dp[i][j] = dp[i][j] || (dp[i][j - 1] && (s3[i + j - 1] == s2[j - 1]));
    44             }
    45         }
    46         int result = dp[len1][len2];
    47         
    48         for (i = 0; i < len1 + 1; ++i) {
    49             dp[i].clear();
    50         }
    51         dp.clear();
    52         
    53         return result == 1;
    54     }
    55 private:
    56     vector<vector<int> > dp;
    57 };

    Solution2:

      Space-optimized version, only O(n) space is needed.

    Accepted code:

     1 // 1AC, space optimized.
     2 class Solution {
     3 public:
     4     bool isInterleave(string s1, string s2, string s3) {
     5         int len1;
     6         int len2;
     7         int len3;
     8         
     9         len1 = (int)s1.length();
    10         len2 = (int)s2.length();
    11         len3 = (int)s3.length();
    12         if (len3 != len1 + len2) {
    13             return false;
    14         }
    15         
    16         if (len1 == 0) {
    17             return s2 == s3;
    18         } else if (len2 == 0) {
    19             return s1 == s3;
    20         }
    21         
    22         if (len1 < len2) {
    23             return isInterleave(s2, s1, s3);
    24         }
    25         
    26         int i, j;
    27         dp.resize(2);
    28         for (i = 0; i < 2; ++i) {
    29             dp[i].resize(len2 + 1);
    30         }
    31         
    32         dp[0][0] = 1;
    33         for (j = 1; j <= len2; ++j) {
    34             if (dp[0][j - 1] && s3[j - 1] == s2[j - 1]) {
    35                 dp[0][j] = 1;
    36             } else {
    37                 dp[0][j] = 0;
    38             }
    39         }
    40         
    41         int flag = 1, nflag = !flag;
    42         for (i = 1; i <= len1; ++i) {
    43             if (dp[nflag][0] && s3[i - 1] == s1[i - 1]) {
    44                 dp[flag][0] = 1;
    45             } else {
    46                 dp[flag][0] = 0;
    47             }
    48             for (j = 1; j <= len2; ++j) {
    49                 dp[flag][j] = 0;
    50                 dp[flag][j] = dp[flag][j] || (dp[nflag][j] && (s3[i + j - 1] == s1[i - 1]));
    51                 dp[flag][j] = dp[flag][j] || (dp[flag][j - 1] && (s3[i + j - 1] == s2[j - 1]));
    52             }
    53             flag = !flag;
    54             nflag = !flag;
    55         }
    56         int result = dp[nflag][len2];
    57         
    58         for (i = 0; i < 2; ++i) {
    59             dp[i].clear();
    60         }
    61         dp.clear();
    62         
    63         return result == 1;
    64     }
    65 private:
    66     vector<vector<int> > dp;
    67 };
  • 相关阅读:
    深入学习图数据库语言Gremlin 系列文章链接汇总
    HugeGraph入门
    Connection 'ens33' is not available on device ens33 because device is strictly unmanaged
    正则里的\s和s有什么区别?
    LXMERT: Learning Cross-Modality Encoder Representations from Transformers
    Pixel-BERT: Aligning Image Pixels with Text by Deep Multi-Modal Transformers
    在vue中使用jsx语法
    JavaScript数据结构和算法
    如何用智能手机或PAD观看笔记本电脑的视频?
    linux shell和windows bat编写
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3568213.html
Copyright © 2011-2022 走看看