[LeetCode] Interleaving String-intertwined String

Source: Internet
Author: User

[LeetCode] Interleaving String-intertwined String
It is clear that the question is whether the string s3 can be composed of s1 and s2. At first glance, it is not very difficult, but it is not that easy to think carefully. The first idea is to traverse s1 and s3, delete the letters in s1 from s3, and then compare the remaining strings with s2. But it is obvious that this idea is wrong, because the same letter may appear in s1 and s2, so that a letter in s3 may match in s1 or s2, therefore, this solution is incorrect. One of the correct solutions to this question is to use dynamic planning to write down how to think about it before listing the answer. Starting from the small scale problem, we should first narrow down the problem. To the minimum, if s1 and s2 have only one character, how can we determine whether s3 is intertwined by s1 and s2? Example-1 Suppose s1 = "a", s2 = "B", 's3 = "AB "'. Then our judgment logic will probably look like this: if the first digit of s1 is the same as that of s3, the second digit of s2 is compared with that of s3, and if so, the second digit is true. If the first digit of s2 is the same as that of s3, the second digit of s1 is compared with that of s3. If no conditions are met, it is not true. If (s1 [0] = s3 [0]) {if (s2 [0] = s3 [1]) {return true ;}} if (s2 [0] = s3 [0]) {if (s1 [0] = s3 [1]) {return true ;}} return false; for example-2, the scale is slightly enlarged. Suppose s1 = "a", s2 = "bb", s3 = "abb ". Let's think about how we can handle this situation? According to the above logic, the written logic is like this: if (s1 [0] = s3 [0]) {if (s2 [0] = s3 [1]) {if (s2 [1] = s3 [2]) {return true ;}} if (s2 [0] = s3 [0]) {if (s1 [0] = s3 [1]) {if (s2 [1] = s3 [2]) {return true ;}} if (s2 [1] = s3 [1]) {if (s1 [0] = s3 [2]) {return true ;}} return false ;} but if you think about it, we only add a character after s2 and s3, and there is no need to start judgment again. In the first example, we already know the result of s1 = "a", s2 = "B", 's3 = "AB" '. If this result is not true, we don't have to make further judgments. If it is true, we only need to judge the newly added characters. If it is equal, it is true. If it is not equal, it is not true. This maximizes the use of existing information to avoid repeated operations. Then we try to save the information in example-1, and we can get the following figure: the meaning of matrix [I] [j], an element in a two-dimensional array of Case-1 matrix, can be understood as: s1.substring (0, I) and s2.substring (0, j) the two strings form a possible combination of s3.substring (0, I + J-1) strings. When the length of any string in s1 or s2 increases, we only need to compare the new characters based on the existing possibility. For example, the preceding two-dimensional array evolved from Case-2 to Case-2 matrix, when we add a 'B' in s2, we only need to calculate martix [0] [2] and martix [1] [2], these two values are evolved based on future data. Matrix [0] [1] => matrix [0] [2], matrix [0] [2] & matrix [1] [1] => matrix [1] [2]. We can conclude from the two examples above that, when a character is added to a string in s1 or s2, we can use the existing data to derive a new result set without performing a lot of calculations. Therefore, we can split common strings into small-scale problems and obtain the final result through this accumulation method. Through the matrix above, we can find that if we want to know whether the string s3 is composed of s1 and s2, and how it is composed, We need to calculate a $ (len (s1) + 1) * (len (s2) + 1) $ matrix, and then calculate the result of matrix [len (s1)] [len (s2)] to obtain the answer. When we have two strings s1 and s2, we treat s1 and s2 as having only one character as the starting character, and then keep the length of s1 or s2 unchanged, the length of another string is gradually increased to fill the matrix. After calculating the entire matrix, we get the answer we need. In practice, let's take s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" as an example. We try to fill in a matrix like this: the full-case-matrix question only requires judgment results, but does not need to list possible combinations. Therefore, we do not need to store combinations, but simply store a Boolean value to determine whether there is a scheme. The Code is as follows: public boolean isInterleave (String s1, String s2, String s3) {if (s1 = null | s2 = null | s3 = null) return false; if (s1.length () + s2.length ()! = S3.length () return false; boolean [] [] matrix = new boolean [s1.length () + 1] [s2.length () + 1]; matrix [0] [0] = true; for (int I = 1; I <= s2.length (); I ++) {if (s2.charAt (I-1) = s3.charAt (I-1) matrix [0] [I] = true; else break;} for (int I = 1; I <= s1.length (); I ++) {if (s1.charAt (I-1) = s3.charAt (I-1) matrix [I] [0] = true; else break ;} for (int I = 1; I <= s1.length (); I ++) {for (int j = 1; j <= s2.length (); j ++) {if (s1.charAt (I-1) = s3.charAt (I + j-1 )) matrix [I] [j] = matrix [I-1] [j] | matrix [I] [j]; if (s2.charAt (j-1) = s3.charAt (I + j-1) matrix [I] [j] = matrix [I] [j-1] | matrix [I] [j];} return matrix [s1.length ()] [s2.length ()];}

Related Article

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.