Good question. String. Linear time.
I think the first person to get this question should not know what to do, or I am too weak. Let's find out what the problem requires. From a long string, the characters in the string contain exactly the characters in the other given target string, and the string is minimal in length.
Another important simplification, which indicates that the requirement for such a shortest string is only one, the restriction in fact implies the overall idea of the problem. Just find a long string, and then cut it down to not be able to reduce it.
From the requirements of the topic can be found that the problem for the character string in the order is not required. So it is very natural to think of using the hash table to save the number of characters in the target string, and then find a string in the source strings. The number of characters included is greater than or equal to the target string. Difficult is difficult in how to achieve this function.
The ability to use the number of characters in a target string as a requirement. Whenever a character is found in a long string, the character exists in the target string. Then demand should be reduced by 1. When the demand equals 0, it means so far. Characters in a long string can exactly match the demand for that character in the target string. Suppose that the requirement of a target string character becomes a negative value, indicating that there is an oversupply of this character in a long string under the current length. In order to describe the simplicity of the narrative, I define when the long string in the target string to the character's demand is positive when the character is provided, in order to meet the rigid demand, whether the supply surplus. Then another question, how do you know that the target string is completely satisfied? Of course you can go to one of the scanning requirements are not all become non-positive, but another simpler way, that is, the length of the target string as a total demand, when the demand for rigidity, the total demand minus 1, when the total demand becomes 0 o'clock, indicating that the target string is satisfied.
The above description of the process in a long string found a word string, can fully satisfy the target string, but not guaranteed to be the shortest, because very many characters are not satisfied with the other characters have been oversupply, how to remove this part of the superfluous? Sweep the seconds long string from the start point onwards. Assuming that the current character does not exist in the target string at all, it can pass directly. The assumption exists in the target string and the supply is excessive. Then this character can be removed from our string, which is the equivalent of shortening our string. But remember to put demand + +. Because of the reduced supply. Knowing a character that exists both in the target string and his demand is exactly equal to 0, we have to stop, because all this time is rigid demand, can no longer reduce the supply.
The code, such as the following, is not optimized. Just the idea is written out.
Class Solution {public:string Minwindow (string S, String T) {int ct1[270], ct2[270]; int mlen1 = S.length (); int mlen2 = T.length (); memset (ct1, 0, sizeof (CT1)); memset (ct2, 0, sizeof (CT2)); int hole = mlen2, minSize = int_max, start = 0, Minstart; for (int i=0;i<mlen2;i++) {ct1[t[i]]++; ct2[t[i]]++; } for (int i=0;i<mlen1;i++) {if (ct2[s[i]]>0) {ct1[s[i]]--; if (ct1[s[i]]>=0) hole--; } if (hole = = 0) {while (START<MLEN1) {if (ct2[s[start]]>0) { if (ct1[s[start]]<0) ct1[s[start]]++; else break; } start++; } if (minsize>i-start+1) {minSize = i-start+1; Minstart = Start; }}} if (minSize = = Int_max) return ""; Return S.substr (Minstart, minSize); }};
Leetcode First brush _minimum Window Substring