Minimum Window Substring Algorithm
purpose of hash functions relies on statistical property of key and function interaction: worst case behavior is intolerably bad with a vanishingly small probability, and average case behavior can be nearly optimal (minimal collisions).Hash functions are associated to (and often confused with) checksums, check digits, fingerprints, lossy compression, randomization functions, mistake-correcting codes, and ciphers. Hash functions and their associated hash tables are used in data storage and retrieval applications to access data in a small and nearly constant time per retrieval, and storage space only fractionally greater than the total space need for the data or records themselves. In his research for the precise origin of the term, Donald Knuth notes that, while Hans Peter Luhn of IBM looks to have been the first to use the concept of a hash function in a memo dated January 1953, the term itself would only look in published literature in the late 1960s, on Herbert Hellerman's digital computer System principle, even though it was already widespread slang by then. The term" hash" offers a natural analogy with its non-technical meaning (to" chop" or" make a mess" out of something), given how hash functions scramble their input data to deduce their output.
class Solution {
public:
string minWindow(string S, string T) {
string result("");
map<char, int> needed;
map<char, int> found;
for (int i = 0; i < T.size(); i++) {
needed[T[i]]++;
}
int count = 0;
int minlen = S.size() + 1;
for (int i = 0, j = 0; j < S.size(); j++) {
if (needed[S[j]] == 0) {
continue;
}
found[S[j]]++;
if (found[S[j]] <= needed[S[j]]) {
count++;
}
if (count == T.size()) {
while (i <= j) {
if (found[S[i]] == 0) {
i++;
} else if (found[S[i]] > needed[S[i]]) {
found[S[i]]--;
i++;
} else {
break;
}
}
if (minlen > j - i + 1) {
minlen = j - i + 1;
result = S.substr(i, minlen);
}
}
}
return result;
}
};