|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +class Solution { |
| 7 | + public String minWindow(String s, String t) { |
| 8 | + if (s.length() < t.length()) { |
| 9 | + return ""; |
| 10 | + } |
| 11 | + |
| 12 | + Map<Character, Integer> charCount = new HashMap<>(); |
| 13 | + for (char ch : t.toCharArray()) { |
| 14 | + charCount.put(ch, charCount.getOrDefault(ch, 0) + 1); |
| 15 | + } |
| 16 | + |
| 17 | + int targetCharsRemaining = t.length(); |
| 18 | + int[] minWindow = { 0, Integer.MAX_VALUE }; |
| 19 | + int startIndex = 0; |
| 20 | + |
| 21 | + for (int endIndex = 0; endIndex < s.length(); endIndex++) { |
| 22 | + char ch = s.charAt(endIndex); |
| 23 | + if (charCount.containsKey(ch) && charCount.get(ch) > 0) { |
| 24 | + targetCharsRemaining--; |
| 25 | + } |
| 26 | + charCount.put(ch, charCount.getOrDefault(ch, 0) - 1); |
| 27 | + |
| 28 | + if (targetCharsRemaining == 0) { |
| 29 | + while (true) { |
| 30 | + char charAtStart = s.charAt(startIndex); |
| 31 | + if (charCount.containsKey(charAtStart) && charCount.get(charAtStart) == 0) { |
| 32 | + break; |
| 33 | + } |
| 34 | + charCount.put(charAtStart, charCount.getOrDefault(charAtStart, 0) + 1); |
| 35 | + startIndex++; |
| 36 | + } |
| 37 | + |
| 38 | + if (endIndex - startIndex < minWindow[1] - minWindow[0]) { |
| 39 | + minWindow[0] = startIndex; |
| 40 | + minWindow[1] = endIndex; |
| 41 | + } |
| 42 | + |
| 43 | + charCount.put(s.charAt(startIndex), charCount.getOrDefault(s.charAt(startIndex), 0) + 1); |
| 44 | + targetCharsRemaining++; |
| 45 | + startIndex++; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return minWindow[1] >= s.length() ? "" : s.substring(minWindow[0], minWindow[1] + 1); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class WrongSolution { // 알고리즘은 맞지만 시간초과 발생... O(n)의 sliding window 방식으로 풀어야 했던 문제.. |
| 54 | + public String minWindow(String s, String t) { |
| 55 | + Map<Character, Integer> targetMap = new HashMap<>(); |
| 56 | + List<Integer> candidates = new ArrayList<>(); |
| 57 | + for (char c : t.toCharArray()) { |
| 58 | + targetMap.put(c, targetMap.getOrDefault(c, 0) + 1); |
| 59 | + } |
| 60 | + |
| 61 | + String minStr = null; |
| 62 | + for (int i = 0; i < s.length(); i++) { |
| 63 | + if (targetMap.containsKey(s.charAt(i))) { |
| 64 | + candidates.add(i); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + int size = candidates.size(); |
| 69 | + |
| 70 | + while (size > 0) { |
| 71 | + Map<Character, Integer> windowMap = new HashMap<>(targetMap); |
| 72 | + int targetCnt = t.length(); |
| 73 | + int startIdx = candidates.remove(0); |
| 74 | + int endIdx = -999; |
| 75 | + |
| 76 | + for (int i = startIdx; i < s.length(); i++) { |
| 77 | + char at = s.charAt(i); |
| 78 | + |
| 79 | + if (windowMap.containsKey(at) && windowMap.get(at) > 0) { |
| 80 | + windowMap.put(at, windowMap.get(at) - 1); |
| 81 | + targetCnt--; |
| 82 | + } |
| 83 | + |
| 84 | + if (targetCnt == 0) { |
| 85 | + endIdx = i + 1; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (targetCnt == 0) { |
| 91 | + if (minStr == null) { |
| 92 | + minStr = s.substring(startIdx, endIdx); |
| 93 | + } else { |
| 94 | + int prevSize = minStr.length(); |
| 95 | + minStr = prevSize < Math.abs(endIdx - startIdx) ? minStr : s.substring(startIdx, endIdx); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + size--; |
| 100 | + } |
| 101 | + |
| 102 | + return minStr == null ? "" : minStr; |
| 103 | + } |
| 104 | +} |
0 commit comments