일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 43. Multiply Strings
- attribute
- Decorator
- Substring with Concatenation of All Words
- kaggle
- 30. Substring with Concatenation of All Words
- shiba
- iterator
- concurrency
- 715. Range Module
- Protocol
- 컴퓨터의 구조
- 운영체제
- 315. Count of Smaller Numbers After Self
- LeetCode
- 밴픽
- Convert Sorted List to Binary Search Tree
- Python Implementation
- Python Code
- t1
- Class
- Python
- data science
- 프로그래머스
- 109. Convert Sorted List to Binary Search Tree
- Regular Expression
- Generator
- 파이썬
- DWG
- 시바견
Archives
- Today
- Total
Scribbling
[Java 101] 76. Minimum Window Substring: Set, HashMap 본문
Computer Science/Java
[Java 101] 76. Minimum Window Substring: Set, HashMap
focalpoint 2023. 2. 22. 02:53class Solution {
public String minWindow(String s, String t) {
String ret = null;
Set<Character> charSet = t.chars().mapToObj(c -> (char) c)
.collect(Collectors.toSet());
List<Integer> indexList = new ArrayList<>();
Map<Character, Integer> need = new HashMap<>();
for (int i=0; i<s.length(); i++) {
Character c = s.charAt(i);
if (charSet.contains(c)) {
indexList.add(i);
}
}
for (int i=0; i<t.length(); i++) {
Character c = t.charAt(i);
need.put(c, need.getOrDefault(c, 0) + 1);
}
int j = 0;
for (int i=0; i<indexList.size(); i++) {
Character c1 = s.charAt(indexList.get(i));
need.put(c1, need.get(c1)-1);
if (need.get(c1) == 0) {
charSet.remove(c1);
}
Character c2 = s.charAt(indexList.get(j));
while (need.get(c2) < 0) {
need.put(c2, need.get(c2)+1);
j += 1;
c2 = s.charAt(indexList.get(j));
}
if (charSet.isEmpty() &&
(ret == null || (indexList.get(i) - indexList.get(j) + 1) < ret.length())) {
ret = s.substring(indexList.get(j), indexList.get(i)+1);
}
}
if (ret == null) return "";
return ret;
}
}
'Computer Science > Java' 카테고리의 다른 글
[Java101] LeetCode: 23. Merge k Sorted Lists: Priority Queue (0) | 2023.03.02 |
---|---|
[Java 101] 20. Valid Parentheses: Stack (0) | 2023.02.23 |
[Java 101] 125. Valid Palindrome with StringBuilder (0) | 2023.02.21 |
[Java 101] 347. Top K Frequent Elements with HashMap as a counter, PriorityQueue, (0) | 2023.02.14 |
[Java 101] 49. Group Anagrams with HashMap, StringBuilder (0) | 2023.02.14 |