• 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 };
  • 相关阅读:
    django DEBUG=False
    Lftp 简单使用步骤
    django admin管理后台中文添加问题
    Rsync同步设置的一例
    在nginx中,禁止IP访问.只可以使用域名访问.
    python imaplib无痕取信的主要
    Centos安装配置Postfix邮件服务器--网址
    还没有写完准备弡上cpickle 还有字典
    python 截取某一天的日志,简单操作
    abp 关闭审计日志
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3568213.html
Copyright © 2020-2023  润新知