Interleave string Analysis

Source: Internet
Author: User

Source of question: "waiting for words", original @ Chen liren. You are welcome to continue to pay attention to the Public Account "" waiting for words"

Three strings A, B, and C. Judge whether C is the interleave of A and B, that is, C should have all the characters in A and B, and the Character Sequence in C is the same as that in A and B. For example,

1. a = "ef" b = "gh" c = "egfh" return true;2. a = "ef" b = "gh" c = "ehgf" return false。

This question does not indicate whether A and B have the same characters, which directly affects the final solution. Therefore, during the interview process, you should interact with the interviewer, find out what to do, and then start. Here we will analyze whether there are identical characters. Hope to help you.

If a and B do not have the same characters

In this case, the processing is relatively simple. to traverse the C string one by one, first process the first character and match it with the character in a. If there is no match, it matches the character in B one by one, if no matching match exists, false is returned. If A or B matches the first character of C, the second character is considered as above, the string that matches the first character of C, also considers the second character, and so on. After C traversal is complete, if a and B still have no characters and false is returned, all traversal is complete, and true is returned. The Code is as follows:

bool interleave_diff(const char* a,const char* b,const char* c){int lenA = strlen(a),lenB = strlen(b),lenC = strlen(c);if(lenA + lenB != lenC)return false;int i = 0,j = 0,k = 0;while(c[k] != '\0'){if(a[i] == c[k]) i++;else if(b[j] == c[k]) j++;else return false;k++;}if(a[i] != '\0' || b[j] != '\0')return false;return true;}

If A and B have the same characters

The preceding algorithm cannot process A and B with the same characters, for example, a = "XXY", B = "xxz", c = "xxzxxy ". If you want to process the same character, it is more direct. If both of them match the characters in C, both of them are considered. If one of them returns true, the entire algorithm returns true.

Therefore, the following describes a dynamic planning solution. A premise that a problem can be solved using dynamic planning is that there must be repeated subproblems. In this way, the sub-problem can be solved and reused later to improve the algorithm efficiency. Is there a subproblem with this question? Consider an extreme example: a = "XXX", B = "XXX", c = "xxxxxx ". Sub-problems. Otherwise, we can draw a recursive tree.

We use the explain table to store the results of the sub-problem. If DP [I] [J] is true, it indicates C [0 .. I + J-1] is a [0 .. i-1] and B [0 .. the interleave string of the J-1. The algorithm implementation process is similar to the above recursion:

Bool interleave_same (const char * a, const char * B, const char * c) {int Lena = strlen (A), lenb = strlen (B ), lenc = strlen (c); If (Lena + lenb! = Lenc) return false; bool DP [Lena + 1] [lenb + 1]; int I, j; for (I = 0; I <= Lena; I ++) {for (j = 0; j <= lenb; j ++) {if (I = 0 & J = 0) DP [I] [J] = true; else if (I = 0) DP [I] [J] = (B [J-1] = C [I + J-1]) & DP [I] [J-1]; else if (j = 0) DP [I] [J] = (a [I-1] = C [I + J-1]) & DP [I-1] [J]; else DP [I] [J] = (A [I-1] = C [I + J-1]) & DP [I-1] [J]) | (B [J-1] = C [I + J-1]) & DP [I] [J-1]); // state transfer equation} return DP [Lena] [lenb];}

If you have any questions, please correct them. Thank you.



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.