|
| 1 | +- λ¬Έμ : https://leetcode.com/problems/minimum-window-substring/ |
| 2 | +- νμ΄: https://algorithm.jonghoonpark.com/2024/07/29/leetcode-76 |
| 3 | + |
| 4 | +## λ²μ 1 |
| 5 | + |
| 6 | +```java |
| 7 | +class Solution { |
| 8 | + public String minWindow(String s, String t) { |
| 9 | + if (s.length() < t.length()) { |
| 10 | + return ""; |
| 11 | + } |
| 12 | + |
| 13 | + Map<Character, Integer> counter = new HashMap<>(); |
| 14 | + |
| 15 | + for (int i = 0; i < t.length(); i++) { |
| 16 | + counter.merge(t.charAt(i), 1, Integer::sum); |
| 17 | + } |
| 18 | + |
| 19 | + int start = 0; |
| 20 | + |
| 21 | + int minLength = 0; |
| 22 | + int minStart = 0; |
| 23 | + |
| 24 | + for (int i = 0; i < s.length(); i++) { |
| 25 | + // update counter |
| 26 | + char currentChar = s.charAt(i); |
| 27 | + |
| 28 | + Integer count = counter.get(currentChar); |
| 29 | + if (count != null) { |
| 30 | + counter.merge(currentChar, -1, Integer::sum); |
| 31 | + } else { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + // check all count is 0 -> update min; |
| 36 | + while (validate(counter)) { |
| 37 | + int currentLength = i + 1 - start; |
| 38 | + if (minLength > currentLength || minLength == 0) { |
| 39 | + minStart = start; |
| 40 | + minLength = currentLength; |
| 41 | + } |
| 42 | + |
| 43 | + // try to move start to start + 1 |
| 44 | + // increase count if front char exist in counter |
| 45 | + count = counter.get(s.charAt(start)); |
| 46 | + if (count != null) { |
| 47 | + if (count == 0) { |
| 48 | + break; |
| 49 | + } |
| 50 | + |
| 51 | + counter.put(s.charAt(start), count + 1); |
| 52 | + } |
| 53 | + start++; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return s.substring(minStart, minStart + minLength); |
| 58 | + } |
| 59 | + |
| 60 | + private boolean validate(Map<Character, Integer> counter) { |
| 61 | + for (int count : counter.values()) { |
| 62 | + if (count > 0) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + return true; |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### TC, SC |
| 72 | + |
| 73 | +λ¬Έμ μ λ€μκ³Ό κ°μ΄ μ μλμ΄ μλ€. |
| 74 | + |
| 75 | +```java |
| 76 | +m == s.length |
| 77 | +n == t.length |
| 78 | +``` |
| 79 | + |
| 80 | +μκ° λ³΅μ‘λλ `O(n * m)`, κ³΅κ° λ³΅μ‘λλ `O(n)` μ΄λ€. |
| 81 | + |
| 82 | +## λ²μ 2: μ±λ₯ μ°μ λ²μ |
| 83 | + |
| 84 | +μ¬μ€ μ΄ λ²μ μ μ€μ μ΄ν리μΌμ΄μ
μμλ μ°μ§ μμμ§λ λͺ¨λ₯΄κ² λ€. μ΄ν΄νλλ° μ’ λ μκ°μ΄ 걸릴 κ² κ°κΈ° λλ¬Έμ΄λ€. |
| 85 | + |
| 86 | +```java |
| 87 | +class Solution { |
| 88 | + public String minWindow(String s, String t) { |
| 89 | + if (s.length() < t.length()) { |
| 90 | + return ""; |
| 91 | + } |
| 92 | + |
| 93 | + Integer[] counter = new Integer[123]; // μ λΉν λλν λ°°μ΄ ν λΉ |
| 94 | + |
| 95 | + char[] mustContainCharArray = t.toCharArray(); |
| 96 | + for (char mustContainChar : mustContainCharArray) { |
| 97 | + int count = counter[mustContainChar] == null ? 0 : counter[mustContainChar]; |
| 98 | + count = count + 1; |
| 99 | + counter[mustContainChar] = count; |
| 100 | + } |
| 101 | + |
| 102 | + int start = 0; |
| 103 | + |
| 104 | + int minLength = 0; |
| 105 | + int minLengthFrom = 0; |
| 106 | + |
| 107 | + for (int i = 0; i < s.length(); i++) { |
| 108 | + char currentChar = s.charAt(i); |
| 109 | + |
| 110 | + Integer count = counter[currentChar]; |
| 111 | + if (count != null) { |
| 112 | + count = count - 1; |
| 113 | + counter[currentChar] = count; |
| 114 | + } |
| 115 | + |
| 116 | + // check all count is 0 -> update min; |
| 117 | + while (validate(counter)) { |
| 118 | + int currentLength = i + 1 - start; |
| 119 | + if (minLength > currentLength || minLength == 0) { |
| 120 | + minLengthFrom = start; |
| 121 | + minLength = currentLength; |
| 122 | + } |
| 123 | + |
| 124 | + // try to move start to start + 1 |
| 125 | + // increase count if front char exist in counter |
| 126 | + count = counter[s.charAt(start)]; |
| 127 | + if (count != null) { |
| 128 | + if (count == 0) { |
| 129 | + break; |
| 130 | + } |
| 131 | + |
| 132 | + counter[s.charAt(start)] = count + 1; |
| 133 | + } |
| 134 | + start++; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return s.substring(minLengthFrom, minLengthFrom + minLength); |
| 139 | + } |
| 140 | + |
| 141 | + private boolean validate(Integer[] counter) { |
| 142 | + for (int i = 65; i < counter.length; i++) { // 65λΆν° μνλ²³μ΄ μμνκΈ° λλ¬Έμ κ·Έ μ΄μ κ°μ μλ―Έμλ€. |
| 143 | + Integer count = counter[i]; |
| 144 | + if (count == null) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + if (count > 0) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + return true; |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### TC, SC |
| 158 | + |
| 159 | +λ¬Έμ μ λ€μκ³Ό κ°μ΄ μ μλμ΄ μλ€. |
| 160 | + |
| 161 | +```java |
| 162 | +m == s.length |
| 163 | +n == t.length |
| 164 | +``` |
| 165 | + |
| 166 | +μκ° λ³΅μ‘λλ `O(n*m)`, κ³΅κ° λ³΅μ‘λλ `O(1)` μ΄λ€. ννμμΌλ‘λ μκ°λ³΅μ‘λλ λμΌνλ μ€μ μ€ν μκ°μ μ€μ΄λ€μκ³ , 곡κ°λ³΅μ‘λλ μ€μ΄λ€κ² λμλ€. |
0 commit comments