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

[Leetcode] Interleaving String

时间:2014-06-12 19:06:19      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

Question:

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 = "aabcc",
s2 = "dbbca",

 

s3 = "aadbbcbcac"为例来看此题。

 

bubuko.com,布布扣

 

 

bubuko.com,布布扣
 1 public class Solution {
 2     public boolean isInterleave(String s1, String s2, String s3) {
 3         if(s1.length()+s2.length()!=s3.length())
 4             return false;
 5         boolean[][] state=new boolean[s1.length()+1][s2.length()+1];
 6         state[0][0]=true;
 7         for(int i=1;i<=s1.length();i++){
 8             state[i][0]=state[i-1][0]&&(s1.charAt(i-1)==s3.charAt(i-1));
 9         }
10         for(int i=1;i<=s2.length();i++){
11             state[0][i]=state[0][i-1]&&(s2.charAt(i-1)==s3.charAt(i-1));
12         }
13         for(int i=1;i<=s1.length();i++){
14             for(int j=1;j<=s2.length();j++){
15                 state[i][j]=(state[i-1][j]&&(s1.charAt(i-1)==s3.charAt(i+j-1)))||(state[i][j-1]&&(s2.charAt(j-1)==s3.charAt(i+j-1)));
16             }
17         }
18         return state[s1.length()][s2.length()];
19     }
20 }
bubuko.com,布布扣

 

 

 

 

 

[Leetcode] Interleaving String,布布扣,bubuko.com

[Leetcode] Interleaving String

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/wolohaha/p/3781762.html

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