leetcode 68: Interleaving String

Interleaving StringAug 31 '12

Given s1, s2, s3, 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.

 .

 

public class Solution {     public boolean isInterleave(String s1, String s2, String s3) {         // Start typing your Java solution below         // DO NOT write main() function         int l1 = s1.length();         int l2 = s2.length();         int l3 = s3.length();                 if(l1+l2!=l3) return false;

                boolean[][] hit = new boolean[l1+1][l2+1];         hit[0][0] = true;         for(int i=1; i<=l1; i++) {             if(s1.charAt(i-1) == s3.charAt(i-1) )                  hit[i][0] = true;         }                         for(int i=1; i<=l2; i++) {             if(s2.charAt(i-1) == s3.charAt(i-1) )                  hit[0][i] = true;         }                 for(int i=1; i<=l1; i++) {             for(int j=1; j<=l2; j++) {                 if(s1.charAt(i-1) == s3.charAt(i+j-1)) {                     hit[i][j] = hit[i-1][j];                 }                 if(s2.charAt(j-1) == s3.charAt(i+j-1) ) {                     hit[i][j] = hit[i][j-1] | hit[i][j];                 }                             }                     }         return hit[l1][l2];     } }


 

posted @ 2013-02-05 08:29  西施豆腐渣  阅读(151)  评论(0编辑  收藏  举报