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

LintCode Interleaving String

时间:2016-08-16 07:03:09      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

Given three strings: s1s2s3, determine whether s3 is formed by the interleaving of s1 and s2.

Example

For s1 = "aabcc", s2 = "dbbca"

  • When s3 = "aadbbcbcac", return true.
  • When s3 = "aadbbbaccc", return false.

 

For this problem, if we think about the problem in the combination way then we have l1 = s1.length(); l2 = s2.length(); l3 = s3.length(); We have O(2^(l3)) ways to chose appropriate char to the certain slot then we can construct the s3. Due to the requirement of the problem we have want to use additional memory to store the repeated calculated result which could be used to solve larger problem with trivial manipulation directly. So this problem, we could use two dimentional array to store if the first i chars from s1 and first j chars from s2 could be used to construct the first i+j chars of s3. The result will be boolean true or false. 

  1. state interleaving[i][j] is the result whether first i chars from s1 and first j chars form s2 could be used to construct the first i+j chars in s3.
  2. interleaving[i][j] = interleaving[i-1][j] && s1.char(i-1) == s3.char(i+j -1) || interleaving[i][j-1] && s2.char(j-1) == s3.char(i+j -1). In this way the result of interleaving[i][j] is depending on: 1) the boolean whether last char of s1 and s3 equals && the situation interleaving[i-1][j] could be achieve 2)the boolean whether last char of s2 and s3 equals && the situation interleaving[i][j-1] could be achievedBecause this problem want we to see if there is way to achieve it, as long as one of the condition satisfied, return true;
  3. Initialize the boolean array. interleaving[0][0] = true
  4. Solve interleaving[l1][l2]
public class Solution {
    /**
     * Determine whether s3 is formed by interleaving of s1 and s2.
     * @param s1, s2, s3: As description.
     * @return: true or false.
     */
    public boolean isInterleave(String s1, String s2, String s3) {
        // write your code here
        int l1 = s1.length();
        int l2 = s2.length();
        //edge case when the l1+l2 does not equals to the s3 length return false
        if (l1 + l2 != s3.length()) {
            return false;
        }
        //Define the state that boolean isInterleave[i][j] stand for i-length s1 
        //and j-length s2 could be combined to construct a i+j-length word s3 
        boolean[][] isInterleave = new boolean[l1+1][l2+1];
        isInterleave[0][0] = true;
        //initialize all situations that use s1 to construct s3
        for (int i = 1; i <= l1; i++) {
            isInterleave[i][0] =  (s1.charAt(i-1) == s3.charAt(i-1))
            && isInterleave[i-1][0];
        }
        //initialize all situations that use s2 to construct s3
        for (int i = 1; i <=l2; i++) {
            isInterleave[0][i] = (s2.charAt(i-1) == s3.charAt(i-1))
            && isInterleave[0][i-1];    
        }
        
        for(int i = 1; i <= l1; i++) {
            for(int j = 1; j <= l2; j++) {
                isInterleave[i][j] = (isInterleave[i-1][j] && s1.charAt(i-1) == s3.charAt(i+j-1)) || ((isInterleave[i][j-1]) && s2.charAt(j-1) == s3.charAt(i+j-1));
            }
        }
        return isInterleave[l1][l2];
    }
}

 

LintCode Interleaving String

标签:

原文地址:http://www.cnblogs.com/ly91417/p/5774904.html

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