Guest User

Untitled

a guest
Feb 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public class Solution {
  2. public string MinWindow(string s, string t) {
  3. if (s == null | t == null || s.Length == 0 || t.Length == 0 || t.Length > s.Length) return "";
  4. int n = s.Length; int m = t.Length;
  5.  
  6. var need = new int[256]; int count = m;
  7. foreach(char c in t) need[c]++;
  8.  
  9. int i = 0; int min_i = 0; long len = s.Length + 1;
  10. for (int j = 0; j < n; j++){
  11. if (need[s[j]]-- >= 1) count--; // Reducing need -affect counter only when need >= 1
  12. while(count == 0){ // All needs met - till need satsifed continue
  13. if (j-i+1 < len) { len = j-i+1; min_i = i; } // need valid
  14. if (need[s[i++]]++ == 0) count++; // Add to need, if need created - next break;
  15. }
  16. }
  17. return len == s.Length + 1 ? "" : s.Substring(min_i, (int)len);
  18. }
  19. }
Add Comment
Please, Sign In to add comment