1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| import java.util.*;
public class _76最小覆盖子串 {
public String minWindow_slip_window_optimize(String s, String t) { if (s.length() < t.length()) return ""; int[] cntT = new int[128]; char[] charArrayS = s.toCharArray(), charArrayT = t.toCharArray(); int less = 0; for (char c : charArrayT) { if (cntT[c] == 0) less++; cntT[c]++; }
int ansLeft = -1, ansRight = charArrayS.length; int left = 0; for (int right = 0; right < charArrayS.length; right++) { char c = charArrayS[right]; cntT[c]--; if (cntT[c] == 0) { less--; } while (less == 0) { if (ansRight - ansLeft > right - left) { ansLeft = left; ansRight = right; } char x = charArrayS[left]; if (cntT[x] == 0) { less++; } cntT[x]++; left++; } } return ansLeft < 0 ? "" : s.substring(ansLeft, ansRight + 1); }
public String minWindow_slip_window_ling(String s, String t) { if (s.length() < t.length()) return ""; int[] cntS = new int[128], cntT = new int[128]; char[] charArrayS = s.toCharArray(), charArrayT = t.toCharArray(); Set<Character> set = new HashSet<>(); for (char c : charArrayT) { set.add(c); cntT[c]++; } List<Character> cList = new ArrayList<>(set);
int ansLeft = -1, ansRight = charArrayS.length; int left = 0; for (int right = 0; right < charArrayS.length; right++) { cntS[charArrayS[right]]++; while (isCovered(cntS, cntT, cList)) { if (ansRight - ansLeft > right - left) { ansLeft = left; ansRight = right; } cntS[charArrayS[left]]--; left++; } } return ansLeft < 0 ? "" : s.substring(ansLeft, ansRight + 1); }
private boolean isCovered(int[] cntS, int[] cntT, List<Character> cList) { for (Character c : cList) { if (cntS[c] == 0 || cntS[c] < cntT[c]) { return false; } } return true; }
private boolean isCovered(int[] cntS, int[] cntT) { for (int i = 'A'; i <= 'Z'; i++) { if (cntT[i] != 0 || cntS[i] < cntT[i]) { return false; } } for (int i = 'a'; i <= 'z'; i++) { if (cntT[i] != 0 || cntS[i] < cntT[i]) { return false; } } return true; }
Map<Character, Integer> ori = new HashMap<Character, Integer>(); Map<Character, Integer> cnt = new HashMap<Character, Integer>();
public String minWindow_gov(String s, String t) { int tLen = t.length(); for (int i = 0; i < tLen; i++) { char c = t.charAt(i); ori.put(c, ori.getOrDefault(c, 0) + 1); } int l = 0, r = -1; int len = Integer.MAX_VALUE, ansL = -1, ansR = -1; int sLen = s.length(); while (r < sLen) { ++r; if (r < sLen && ori.containsKey(s.charAt(r))) { cnt.put(s.charAt(r), cnt.getOrDefault(s.charAt(r), 0) + 1); } while (check() && l <= r) { if (r - l + 1 < len) { len = r - l + 1; ansL = l; ansR = l + len; } if (ori.containsKey(s.charAt(l))) { cnt.put(s.charAt(l), cnt.getOrDefault(s.charAt(l), 0) - 1); } ++l; } } return ansL == -1 ? "" : s.substring(ansL, ansR); }
public boolean check() { Iterator iter = ori.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Character key = (Character) entry.getKey(); Integer val = (Integer) entry.getValue(); if (cnt.getOrDefault(key, 0) < val) { return false; } } return true; }
public String minWindow(String s, String t) { if (s.length() < t.length()) return ""; Map<Character, Integer> mapS = new HashMap<>(); Map<Character, Integer> mapT = new HashMap<>(); char[] charArrayS = s.toCharArray(); char[] charArrayT = t.toCharArray(); for (char c : charArrayT) { updateMap(mapT, c, true); } Set<Character> setT = mapT.keySet();
int left = 0, right = 0; String res = s + "1"; while (left <= right) { if (compareMap(mapS, mapT)) { if (res.length() > (right - left)) res = s.substring(left, right); while (!setT.contains(charArrayS[left]) && left < right) { left++; if (res.length() > (right - left)) res = s.substring(left, right); } updateMap(mapS, charArrayS[left], false); left++; } else { if (right == s.length()) break; while (right < charArrayS.length - 1 && !setT.contains(charArrayS[right])) { right++; } updateMap(mapS, charArrayS[right], true); right++; } } return Objects.equals(res, s + "1") ? "" : res; }
private boolean compareMap(Map<Character, Integer> mapS, Map<Character, Integer> mapT) { for (Character key : mapT.keySet()) { Integer counterT = mapT.get(key); Integer counterS = mapS.getOrDefault(key, 0); if (counterS < counterT) return false; } return true; }
private void updateMap(Map<Character, Integer> map, char c, boolean isInput) { int counter = map.getOrDefault(c, 0); if (isInput) { counter++; } else { counter--; } if (counter == 0) { map.remove(c); } else { map.put(c, counter); } }
public static void main(String[] args) { _76最小覆盖子串 solution = new _76最小覆盖子串();
System.out.println(solution.minWindow_slip_window_optimize("a", "b")); } }
|