[email protected] [97] Interleaving Strings

https://leetcode.com/problems/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.

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if(s1.length() + s2.length() != s3.length()) return false;
        if(s1.length() == 0 && s2.length() == 0 && s3.length() == 0) return true;

        vector<vector<bool> > dp(s1.length()+1, vector<bool>(s2.length()+1, false));
        for(int i=1;i<=s1.length();++i) {
            dp[i][0] = (s1.substr(0,i) == s3.substr(0,i))? true: false;
        }
        for(int j=1;j<=s2.length();++j) {
            dp[0][j] = (s2.substr(0,j) == s3.substr(0,j))? true: false;
        }

        for(int i=1;i<=s1.length();++i) {
            for(int j=1;j<=s2.length();++j) {
                if(dp[i-1][j] && s1[i-1] == s3[i+j-1]) dp[i][j] = true;
                else if(dp[i][j-1] && s2[j-1] == s3[i+j-1]) dp[i][j] = true;
                else dp[i][j] = false;
            }
        }

        return dp[s1.length()][s2.length()];
    }
};

时间: 2024-10-16 13:59:30

[email protected] [97] Interleaving Strings的相关文章

[email&#160;protected]:php

curl 获取页面信息 function curl_get_content($url){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) App

Ansi[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(五)

无书面许可请勿转载 高级Playbook Extra variables You may have seen in our template example in the previous chapter that we used a variable called group_names . This is one of the magic variables that are provided by Ansible itself. At the time of writing there a

(转)MVC语法[email&#160;protected]和@functions(Razor内定义函数)

(转)MVC语法[email protected]和@functions(Razor内定义函数) 转自:http://www.mikesdotnetting.com/Article/173/[email protected]@Functions-In-WebMatrix The Difference Between @Helpers and @Functions In WebMatrix This is another post which was inspired by a recent qu

Struts2报错异常Method &quot;setUser&quot; failed for object [email&#160;protected]

在写类型转换的时候发现报错 异常信息: 1 ognl.MethodFailedException: Method "setUser" failed for object [email protected] [java.lang.NoSuchMethodException: com.mikey.action.ConverterAction.setUser([Ljava.lang.String;)] 2 at ognl.OgnlRuntime.callAppropriateMethod(O

$*和[email&#160;protected]之间区别代码分析

#!/bin/bash set 'apple pie' pears peaches for i in $*           /*单引号被去掉,循环单个字符输出*/ do echo $i done [[email protected] Ex_14.02-14.31]# sh 14-14-1 apple pie pears peaches -------------------------------------------------------------- #!/bin/bash set

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(六)

无书面许可请勿转载 高级playbook Finding files with variables All modules can take variables as part of their arguments by dereferencing them with {{ and }} . You can use this to load a particular file based on a variable. For example, you might want to select a

【转载】 ERROR 1045 (28000): Access denied for user [email&#160;protected] (using password: NO)

来自:http://www.jb51.net/LINUXjishu/10981.html 错误描述: Mysql中添加用户之后可能出现登录时提示ERROR 1045 (28000): Access denied for user的错误.删除user.user中值为NULL的,或更新NULL为test 1)delete from user where user is NULL 2)update user set user='test' where user is NULL.意外的情况: 如果上述方

[[email&#160;protected]] Omit catch error block if not needed

From [email protected], you can omit catch error block. Before: try { throw new Error('whatever'); } catch(err) { console.log(err) } Now: try { throw new Error('whatever'); } catch { console.log("error happened") } It is just a syntax sugar, if

[email&#160;protected]动态代理-类加载器

一.测试单元     概述:用于测试JAVA代码的工具类,已内置在Eclipse中;     格式:         1.在方法的上面添加@Test;         2.对被测试的方法的要求:权限-public;返回值-void;参数-空参         [email protected]:在@Test标注的方法前执行,可以用于初始化;           @After:在@Test标注的方法后执行,可以用于释放资源; 二.注解     概述:java的一种数据类型,和类/接口在同一级别