Advertisement
ogv

Untitled

ogv
Oct 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. // Runtime: 9 ms, beats 69.12 % of java submissions.
  2. class Solution {
  3.     public String minWindow(String s, String t) {
  4.         int tLength = t.length();
  5.        
  6.         Map<Character, Integer> map = new HashMap<>();
  7.         int[] tCount = new int[tLength];
  8.        
  9.         int charCount = 0;
  10.         for (int i = 0; i < tLength; i++) {
  11.             char c = t.charAt(i);
  12.             Integer ix = map.get(c);
  13.             if (ix == null){
  14.                 ix = charCount++;
  15.                 map.put(c, ix);
  16.             }
  17.             tCount[ix] += 1;
  18.         }
  19.        
  20.         int[] sArray = new int[s.length()];
  21.         for (int i=0; i < s.length(); i++){
  22.             Integer ix = map.get(s.charAt(i));
  23.             sArray[i] = ix != null ? ix: -1;
  24.         }
  25.        
  26.         int minLength = Integer.MAX_VALUE;
  27.         int minPos = 0;
  28.        
  29.         LinkedList<Integer> idx = new LinkedList<Integer>();
  30.         int[] presence = new int[charCount];
  31.         int matchCount = 0;
  32.        
  33.         for (int i = 0; i < sArray.length; i++) {
  34.             int c = sArray[i];        
  35.             if (c == -1) continue;
  36.          
  37.             int first = idx.isEmpty() ? -1: sArray[idx.getFirst()];            
  38.                      
  39.             while (idx.size() > 2 && presence[first] > tCount[first]) {
  40.                 idx.removeFirst();
  41.                 presence[first] -= 1;
  42.                 first = sArray[idx.getFirst()];
  43.             }      
  44.            
  45.             if (first == c && presence[first] >= tCount[first]) {
  46.                 idx.removeFirst();                                    
  47.             }
  48.             else {
  49.                 if (presence[c] < tCount[c]) matchCount++;                
  50.                 presence[c] += 1;                
  51.             }
  52.             idx.add(i);
  53.                        
  54.             if (matchCount == tLength) {
  55.                 int len = idx.getLast() - idx.getFirst() + 1;
  56.                 if (len < minLength) {
  57.                     minLength = len;
  58.                     minPos = idx.getFirst();
  59.                 }
  60.                
  61.                 idx.removeFirst();
  62.                 if (first != -1) presence[first] -= 1;
  63.                
  64.                 matchCount--;
  65.             }
  66.         }
  67.        
  68.         if (minLength == Integer.MAX_VALUE) return "";
  69.        
  70.         return s.substring(minPos, minPos + minLength);    
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement