Leetcode: interleaving string

Source: Internet
Author: User
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.

Difficulty: 87. This is a question about string operations. The requirement is to judge whether a string can be constructed by two strings in their own order by selecting one character from the two strings each time.
It is easy to think of dynamic planning to determine whether or not a certain number of questions can be completed according to certain rules.

Dynamic Planning focuses on finding the Maintenance Quantity and recurrence. The Maintenance Quantity is recursive, and the expected result is often obtained.

Let's talk about the maintenance volume first, res [I] [J] indicates whether the first I character of S1 and the first J character of S2 can be used to indicate the first I + J characters of S3 according to the rule, the final result is res [s1.length ()] [s2.length ()], and you can determine whether it is true or not. Next, let's look at the recursive formula. If we know all the historical information before res [I] [J], how can we get Res [I] [J]? We can see that there are actually only two types of recursion. One is to select the S1 character as the newly added character of S3, and the other is to select the S2 character as the new character. To check whether it can be selected, it is to determine whether the I (j) character of S1 (S2) is equal to the I + J character of S3. If you can select and the corresponding res [I-1] [J] (RES [I] [J-1]) is also true, it means that S3's I + J characters can be expressed. In either case, the res [I] [J] is true and is an or relation. So the recursive formula can be expressed

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)

In terms of time, because it is a two-dimensional dynamic planning, the complexity is O (M * n), and m and n are the lengths of S1 and S2, respectively. The last step is the space cost. We can see that only the information of the previous row is used in the recursive formula. Therefore, we only need a one-dimensional array to maintain the historical information, we put the short string in the inner loop, so that we can only need the length of the short string, so the complexity is O (min (m, n )).

 1 public class Solution { 2     public boolean isInterleave(String s1, String s2, String s3) { 3         if (s3.length() != s1.length() + s2.length()) return false; 4         boolean[][] res = new boolean[s1.length()+1][s2.length()+1]; 5         res[0][0] = true; 6         return helper(s1.length(), s2.length(), res, s1, s2, s3); 7     } 8      9     public boolean helper(int n1, int n2, boolean[][] res, String s1, String s2, String s3) {10         if (res[n1][n2]) return true;11         if (n1 > 0) {12             res[n1-1][n2] = helper(n1-1, n2, res, s1, s2, s3);13         }14         if (n2 > 0) {15             res[n1][n2-1] = helper(n1, n2-1, res, s1, s2, s3);16         }17         if (n1>0 && res[n1-1][n2] && s1.charAt(n1-1)==s3.charAt(n1+n2-1) || n2>0 && res[n1][n2-1] && s2.charAt(n2-1)==s3.charAt(n1+n2-1)) {18             return true;19         }20         else return false;21     }22 }

Before using this method, I also tried to use recursion. As a result, the price of recursion is indeed higher than that of iteration.

 1 public class Solution { 2     public boolean isInterleave(String s1, String s2, String s3) { 3         if (s3.length() != s1.length() + s2.length()) return false; 4         boolean[][] res = new boolean[s1.length()+1][s2.length()+1]; 5         boolean[][] visited = new boolean[s1.length()+1][s2.length()+1]; 6         res[0][0] = true; 7         return helper(s1.length(), s2.length(), res, s1, s2, s3); 8     } 9     10     public boolean helper(int n1, int n2, boolean[][] res, String s1, String s2, String s3) {11         if (res[n1][n2]) return true;12         if (n1 > 0) {13             res[n1-1][n2] = helper(n1-1, n2, res, s1, s2, s3);14         }15         if (n2 > 0) {16             res[n1][n2-1] = helper(n1, n2-1, res, s1, s2, s3);17         }18         if (n1>0 && res[n1-1][n2] && s1.charAt(n1-1)==s3.charAt(n1+n2-1) || n2>0 && res[n1][n2-1] && s2.charAt(n2-1)==s3.charAt(n1+n2-1)) {19             return true;20         }21         else return false;22     }23 }

 

Leetcode: interleaving string

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.