What is the minimum substring length of a string that contains all the characters of another string?

Source: Internet
Author: User

Question:
Given A string and A string set A, evaluate the shortest substring length of the string containing all characters in.
 
Solution 1:
The most direct method is to directly start traversing: Find whether any two substrings contain str2. If any, record the length and obtain the minimum value.
Str1 = "daebfacba ";
Str2 = "abc ";
MinLen = len (str1 );
For I = 0: len (str1)
For j = I + 1: len (str1)
If (isContainAllElements (str1, str2, I, j) // record length if the substring from I to j contains all characters
If j-I <minLen
MinLen = j-I;
Considering isContainAllElements (), the complexity is O (n3 ).
 
Solution 2:
Set a variable for each character in str2 to store the latest position in str1. H (x) is used to represent the value.
Take "daebfacba" as an example,
1. When the first a is scanned, H (a) = 1; H (B), H (c) has no value;
2. When the first B is scanned, H (a) = 1; H (B) = 3, H (c) has no value;
3. when the second a is scanned, because C does not have a value, it overwrites the value of a: H (a) = 5; H (B) = 3, H (c) no value;
4. when the first c is scanned, H (a) = 5; H (B) = 3, H (c) = 6. Then, the distance is calculated once, that is, use the maximum value minus the minimum value of 3;
5. continue scanning. H (a) = 5; H (B) = 7, H (c) = 6, and the distance is 7-5 = 2;
6. continue scanning. H (a) = 8; H (B) = 7, H (c) = 6, and the distance is 8-6 = 2;
The minimum value is 2.
 
The complexity of this algorithm is O (n ).
In this algorithm, the corresponding positions of all characters are not found and can be overwritten directly. The simple feasibility of overwriting is as follows:
Assume that a appears twice, and B and c have three relative positions:
(1) a... B... a... c...
In this case, the second a will overwrite the first a, but since c is behind the second a, you only need to calculate the second a. The overwrite is acceptable.
(2) a... B... c......
At this time, both a will participate in the calculation, and there is no coverage problem.
(3) a... B... c...
In this case, it is obvious that only the second a needs to be calculated, and overwriting is acceptable.
Therefore, to sum up, the coverage method is feasible.
The general idea is:
1. Scan the string. If a character in the set is encountered, the position of the character in the set is recorded.
2. If all characters are not found, fill them in or overwrite them;
If all are found, the distance is calculated and compared to the shortest distance. Otherwise, fill in according to the above rules;
3. Scan the string to get the shortest length of the substring, and record the positions of all characters.
 
Expansion:
Question:
Given A string and A string set A, calculate the maximum length of the string containing all characters in A (the characters in A cannot be repeated in the substring ). Www.2cto.com
For example, "ababcda" and "abc"
The rule is as follows:
1. if each character is not filled with a value:
(1) If the value corresponding to the current character is not entered, enter it directly;
(2) If the corresponding value of the current character has been filled (for example, H (a) = 2), The filled value before position a 2 and 2 is cleared, and re-enter the value of a (for example, H (a) = 4); (because there are duplicates, you need to clear the previous)
 

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.