Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public class Solution {
  2. public String minWindow(String S, String T) {
  3. if(T.isEmpty() || S.isEmpty()) {
  4. return "";
  5. }
  6. int tcount = 0;
  7. int minLen = Integer.MAX_VALUE;
  8. int[] needToFind = new int[128];
  9. int[] hasFound = new int[128];
  10. for(int i=0; i<T.length(); i++) {
  11. int c = T.charAt(i);
  12. needToFind[c]++;
  13. }
  14. int l = 0, r = 0;
  15. int minl = 0, minr = 0;
  16. for(; r<S.length(); r++) {
  17. int rc = S.charAt(r);
  18. if(needToFind[rc] > 0) {
  19. hasFound[rc]++;
  20. if(needToFind[rc] >= hasFound[rc]) {
  21. tcount++;
  22. }
  23. if(tcount == T.length()) {
  24. int lc = S.charAt(l);
  25. while(needToFind[lc] == 0 || needToFind[lc] < hasFound[lc]) {
  26. if(needToFind[lc] < hasFound[lc]) {
  27. hasFound[lc]--;
  28. }
  29. l++;
  30. lc = S.charAt(l);
  31. }
  32. if(r - l + 1 < minLen) {
  33. minr = r;
  34. minl = l;
  35. minLen = r - l + 1;
  36. }
  37. }
  38. }
  39. }
  40. if(tcount != T.length()) {
  41. return "";
  42. } else {
  43. return S.substring(minl, minr+1);
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement