Function
| 3 | |
| 4 | // Function |
| 5 | string Minimum_Window(string s, string t) |
| 6 | { |
| 7 | |
| 8 | int m[256] = { 0 }; |
| 9 | |
| 10 | // Answer |
| 11 | int ans = INT_MAX; // length of ans |
| 12 | int start = 0; // starting index of ans |
| 13 | int count = 0; |
| 14 | |
| 15 | // creating map |
| 16 | for (int i = 0; i < t.length(); i++) { |
| 17 | if (m[t[i]] == 0) |
| 18 | count++; |
| 19 | m[t[i]]++; |
| 20 | } |
| 21 | |
| 22 | // References of Window |
| 23 | int i = 0; |
| 24 | int j = 0; |
| 25 | |
| 26 | // Traversing the window |
| 27 | while (j < s.length()) { |
| 28 | // Calculations |
| 29 | m[s[j]]--; |
| 30 | if (m[s[j]] == 0) |
| 31 | count--; |
| 32 | |
| 33 | // Condition matching |
| 34 | if (count == 0) { |
| 35 | while (count == 0) { |
| 36 | // Sorting ans |
| 37 | if (ans > j - i + 1) { |
| 38 | ans = min(ans, j - i + 1); |
| 39 | start = i; |
| 40 | } |
| 41 | // Sliding I |
| 42 | // Calculation for removing I |
| 43 | |
| 44 | m[s[i]]++; |
| 45 | if (m[s[i]] > 0) |
| 46 | count++; |
| 47 | |
| 48 | i++; |
| 49 | } |
| 50 | } |
| 51 | j++; |
| 52 | } |
| 53 | |
| 54 | if (ans != INT_MAX) |
| 55 | return s.substr(start, ans); |
| 56 | else |
| 57 | return "-1"; |
| 58 | } |
| 59 | |
| 60 | main() |
| 61 | { |