Guest User

Untitled

a guest
Dec 13th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. class Solution {
  2. public String minWindow(String s, String t) {
  3. if(s.length()<t.length())
  4. return "";
  5.  
  6. int[] sChars = new int[256];
  7. int[] tChars = new int[256];
  8. int count = 0;
  9. int start = 0;
  10. int minLength = Integer.MAX_VALUE;
  11. int start_index = -1;
  12.  
  13. for(char c : t.toCharArray()){
  14. tChars[c]++;
  15. }
  16.  
  17. for(int i=0;i<s.length();i++){
  18. sChars[s.charAt(i)]++;
  19. //Check if this char is present in pattern
  20. // We check for less than condition only .. lets say s=aaabcd and t=aabc
  21. // in this case we do not increase the count for 3rd a since that is not required in the pattern
  22. if(tChars[s.charAt(i)]!=0 && sChars[s.charAt(i)]<=tChars[s.charAt(i)])
  23. count++;
  24.  
  25. if(count==t.length()){
  26. while(sChars[s.charAt(start)]>tChars[s.charAt(start)] || tChars[s.charAt(start)]==0){
  27. //If we are moving the start ahead, we are no longer counting the char at that position
  28. //Hence we reduce the count for futher iterations
  29. if(sChars[s.charAt(start)]>tChars[s.charAt(start)])
  30. sChars[s.charAt(start)]--;
  31.  
  32. start++;
  33. }
  34. if(i-start+1<minLength){
  35. minLength = i-start+1;
  36. start_index = start;
  37. }
  38. }
  39. }
  40. if(start_index==-1)
  41. return "";
  42. return s.substring(start_index,start_index+minLength);
  43.  
  44. }
  45. }
Add Comment
Please, Sign In to add comment