About algorithm topics for string manipulation

Source: Internet
Author: User

Original title Link: http://oj.leetcode.com/problems/interleaving-string/

This is a question about string manipulation, and the requirement is to determine whether a string can be constructed from one character in two strings at a time by two strings in their own order.

It is easy to think of dynamic programming as to whether or not a certain number of questions can be fulfilled by some sort of rule.

First, the maintenance volume, res[i][j] means that the first and last J characters of the S1 and S2 can be represented according to the rules of S3 before I+j characters, so the final result is res[s1.length ()][s2.length ()), to determine whether it is true. The next step is recursion, assuming that all the historical information before Res[i][j is known, how do we get res[i][j]. It can be seen that there are only two ways to recursion, one is to select S1 characters as S3 new characters, and the other is to select S2 characters as new characters. and to see if you can choose, is to determine whether S1 (S2) of the first (j) characters and S3 I+j Word story, and so on. If it can be selected and the corresponding Res[i-1][j] (res[i][j-1) is true, the i+j character of the S3 can be represented. As long as there is a form of these two situations, it means that res[i][j] is true and is a relationship. So the recursive type can be expressed as

RES[I][J] = Res[i-1][j]&&s1.charat (i-1) ==s3.charat (i+j-1) | | Res[i][j-1]&&s2.charat (j-1) ==s3.charat (i+j-1)

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Time because it is a two-dimensional dynamic programming, so the complexity is O (m*n), M and N are the lengths of S1 and S2 respectively. The last is the cost of space, you can see that in the recursive type only need to use the last line of information, so we only need a one-dimensional array to complete the maintenance of historical information, in order to more optimization, we put short strings in the inner loop, so that you can only need the length of short strings, so the complexity is O (min (m,n ))。 The code is as follows:

public boolean Isinterleave (string s1, String s2, string s3) {  
    if (s1.length () +s2.length ()!=s3.length ())  
        return false;  
    String Minword = S1.length () >s2.length ()? s2:s1;  
    String Maxword = S1.length () >s2.length ()? s1:s2;  
    boolean[] res = new Boolean[minword.length () +1];  
    Res[0] = true;  
    for (int i=0;i<minword.length (); i++)  
    {  
        res[i+1] = res[i] && minword.charat (i) ==s3.charat (i);  
    } for  
    (int i=0;i<maxword.length (); i++)  
    {  
        Res[0] = res[0] && maxword.charat (i) ==s3.charat (i);  
        for (int j=0;j<minword.length (); j + +)  
        {  
            res[j+1] = Res[j+1]&&maxword.charat (i) ==s3.charat (i+j+ 1) | | Res[j]&&minword.charat (j) ==s3.charat (i+j+1);  
        }  
    Return Res[minword.length ()];  
}

Dynamic Planning In fact, there is a routine, nothing more than to find maintenance, and then get the recursive type, and then look at the historical information for space needs, as far as possible optimization, will be in the back of the dynamic planning to do a more general summary ha.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.