[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.

    bool isInterleave(string s1, string s2, string s3) {
        
        // if length does not match
        if((s1.length()+s2.length())!=s3.length())
            return false;
        
        if(s1.length()==0)
            if(s2.compare(s3)==0)
                return true;
            else
                return false;
                
        if(s2.length()==0)
            if(s1.compare(s3)==0)
                return true; 
            else
                return false;
        
        // if the first char does not match
        if(s1.at(0)!=s3.at(0) && s2.at(0)!=s3.at(0))
            return false;
        
        // if the first char of s1 matches with the first char of s3
        if(s1.at(0)==s3.at(0) && s2.at(0)!=s3.at(0) )
            return isInterleave(s1.substr(1,s1.length()-1), s2, s3.substr(1, s3.length()-1) );
        
        // if the first char of s2 matches with the first char of s3
        if( s1.at(0)!=s3.at(0) && s2.at(0)==s3.at(0) )
            return isInterleave(s1, s2.substr(1,s2.length()-1), s3.substr(1, s3.length()-1) );
            
        // if the first char of s1 and s2 matches with the first char of s3
        if(s1.at(0)==s3.at(0) && s2.at(0)==s3.at(0))
            return (isInterleave(s1.substr(1,s1.length()-1), s2, s3.substr(1, s3.length()-1) ) ) ||( isInterleave(s1, s2.substr(1,s2.length()-1), s3.substr(1, s3.length()-1) ) );
    }
在Leetcode的服务器上,在测试非常长的字符串时,上面的代码给出了超时的错误。

可以考虑用动态规划的思想,把上面递归的中间结果存在一个表中。代码如下:

    bool isInterleave(string s1, string s2, string s3) {
        
        if((s1.length()+s2.length())!=s3.length())
            return false;
        
        vector<bool> table_row (s2.length()+1, false);
        vector<vector<bool>> table(s1.length()+1, table_row ); // this table stores the intermediate results

        for(int i=0; i<=s1.length(); i++)
            if(s1.substr(0,i).compare(s3.substr(0,i)) ==0)
                table[i][0] = true; // initialize the table
        
        for(int j=0; j<=s2.length(); j++)
            if(s2.substr(0,j).compare(s3.substr(0,j)) ==0)
                table[0][j] = true; // initialize the table
        
        for(int i=1; i<=s1.length(); i++)
            for(int j=1; j<=s2.length(); j++)
            {
                if( s1.at(i-1)==s3.at(i+j-1) && s2.at(j-1)!=s3.at(i+j-1) )
                    table[i][j] = table[i-1][j];
                    
                if( s1.at(i-1)!=s3.at(i+j-1) && s2.at(j-1)==s3.at(i+j-1) )
                    table[i][j] = table[i][j-1];
                    
                if( s1.at(i-1)==s3.at(i+j-1) && s2.at(j-1)==s3.at(i+j-1) )
                    table[i][j] = table[i-1][j] || table[i][j-1];
            }        
    
        return table[s1.length()][s2.length()];
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值