Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public string MinWindow(string s, string t) {
- if (s == null | t == null || s.Length == 0 || t.Length == 0 || t.Length > s.Length) return "";
- int n = s.Length; int m = t.Length;
- var need = new int[256]; int count = m;
- foreach(char c in t) need[c]++;
- int i = 0; int min_i = 0; long len = s.Length + 1;
- for (int j = 0; j < n; j++){
- if (need[s[j]]-- >= 1) count--; // Reducing need -affect counter only when need >= 1
- while(count == 0){ // All needs met - till need satsifed continue
- if (j-i+1 < len) { len = j-i+1; min_i = i; } // need valid
- if (need[s[i++]]++ == 0) count++; // Add to need, if need created - next break;
- }
- }
- return len == s.Length + 1 ? "" : s.Substring(min_i, (int)len);
- }
- }
Add Comment
Please, Sign In to add comment