码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode[97]Interleaving String

时间:2015-02-09 14:05:55      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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.

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if(s1.size()+s2.size()!=s3.size())return false;
        vector<vector<bool>> f(s1.size()+1,vector<bool> (s2.size()+1,false));
        f[0][0]=true;
        for(int i=1;i<=s1.size();i++)
        {
            f[i][0]=(f[i-1][0]&&s1[i-1]==s3[i-1]);
        }
        for(int j=1;j<=s2.size();j++)
        {
            f[0][j]=(f[0][j-1]&&s2[j-1]==s3[j-1]);
        }
        for(int i=1;i<=s1.size();i++)
        {
            for(int j=1;j<=s2.size();j++)
            {
                f[i][j]=(f[i][j-1]&&s2[j-1]==s3[i+j-1])||(f[i-1][j]&&s1[i-1]==s3[i+j-1]);
            }
        }
        return f[s1.size()][s2.size()];
    }
};

 

leetcode[97]Interleaving String

标签:

原文地址:http://www.cnblogs.com/Vae98Scilence/p/4281373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!