leetcode-Interleaving String

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.

这道题需要判断两个字符串能否合并组成另外一个新的字符串,这个合并是有序的合并,就是在新的字符串中,可以分别找到跟原来两个字符串,并且字母的顺序跟原两个字符串字母顺序是一致的。

假设s1长度为n1, s2长度为n2,那么s3的每个字母都有2种选择,即s3的第一个字母可以从s1开头取,也可以从s2开头取,这样一共要从s1和s2中取(n1+n2)个数,总共s3可能数为从n1+n2中取n1个.

这里是要判断s3是否可以由s1和s2有序合并。容易想到用递归来求解,比较s3的第一个字母与s1和s2的第一个字母,如果s3[0]=s1[0],说明s3的第一个数可以从s1取,这样要继续搜索s3后面的字符串能否用s1的第一个字母后的字符串和s2拼成;如果s3[0]=s2[0],说明s3的第一个数也可以从s2取,然后继续搜索s3后面字符串能否用s1和s2第一个字母后字符串拼成。这样递归下去是可以算出答案的。

所以编个简单的递归如下:

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s3.size() != s1.size()+s2.size())
            return false;
        return isInterleave(s1, 0, s2, 0, s3, 0);
    }
private:
    bool isInterleave(const string &s1, int b1, const string &s2, int b2, 
         const string& s3, int b3) {
        if (b1 == s1.size())   // s1 reaches end
            return s3.substr(b3) == s2.substr(b2);
        
        if (b2 == s2.size())   // s2 reaches end
            return s3.substr(b3) == s1.substr(b1);
        
         if (s3[b3] == s1[b1]) { //match s1
             if (isInterleave(s1, b1+1, s2, b2, s3, b3+1))
                 return true;
         }
         
         if (s3[b3] == s2[b2]) { //match s2
            if (isInterleave(s1, b1, s2, b2+1, s3, b3+1))
                return true;
         }
        
        return false;  // no match
    }
};
最终结果是TLE。因为上面的递归计算了很多重复的子任务,所以改成记忆化搜索如下:
class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s3.size() != s1.size()+s2.size())
            return false;
        memset(hasSearched, false, sizeof(hasSearched));
        return isInterleave(s1, 0, s2, 0, s3, 0);
    }
private:
    bool isInterleave(const string &s1, int b1, const string &s2, int b2, 
         const string& s3, int b3) {
        if (b1 == s1.size())   // s1 reaches end
            return s3.substr(b3) == s2.substr(b2);
        
        if (b2 == s2.size())   // s2 reaches end
            return s3.substr(b3) == s1.substr(b1);
        
         if (s3[b3] == s1[b1]) { //match s1
             if (!hasSearched[b1+1][b2] && isInterleave(s1, b1+1, s2, b2, s3, b3+1))
                 return true;
         }
         
         if (!hasSearched[b1][b2+1] && s3[b3] == s2[b2]) { //match s2
            if (isInterleave(s1, b1, s2, b2+1, s3, b3+1))
                return true;
         }
        
        hasSearched[b1][b2] = true;
        return false;  // no match
    }
    
    bool hasSearched[100][100];
};

也还可以再继续优化,比如检测到s3字符与s1字符相等时,可以先行判断s3后面的一个字符是否与s1、s2的前面字符相等,如果不等,这条路径就不必走下去了。

其实也可以用动态规划求解。

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s3.size() != s1.size()+s2.size())
            return false;
            
        bool d[200][200] = {false};    // d[i][j]表示s1的前i个字符和s2的前j个字符能否组成s3的前i+j个字符
        d[0][0] = true;   
            
        int len1=s1.size(), len2=s2.size();
        
        for (int i=0; i<=len1; ++i) {
            for (int j=0; j<=len2; j++) {
                
                if (s1[i]==s3[i+j] && d[i][j]==true) 
                    d[i+1][j] = true;
                
                if (s2[j]==s3[i+j] && d[i][j]==true)
                    d[i][j+1] = true;
            }
        }
        
        return d[len1][len2];
    }
    
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值