-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumWindowSubstring.java
More file actions
64 lines (60 loc) · 2.4 KB
/
MinimumWindowSubstring.java
File metadata and controls
64 lines (60 loc) · 2.4 KB
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
package leetcode.string;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
public class MinimumWindowSubstring {
public String minWindow(String S, String T) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if (S == null || T == null || S.length() == 0 || T.length() == 0 || S.length() < T.length())
return "";
HashSet<Character> wholeLetters = new HashSet<Character>();
HashSet<Character> notFound = new HashSet<Character>();
for (char c : T.toCharArray()) {
wholeLetters.add(c);
notFound.add(c);
}
String minWindow = "";
int leftIndex = -1;
Queue<Character> foundQueue = new LinkedList<Character>();
HashMap<Character, Integer> found = new HashMap<Character, Integer>();
for (int i = 0; i < S.length(); i++) {
if (wholeLetters.contains(S.charAt(i))) {
if(notFound.contains(S.charAt(i))){
notFound.remove(S.charAt(i));
found.put(S.charAt(i), i);
foundQueue.add(S.charAt(i));
if (found.size() == 1)
leftIndex = i;
} else{
found.put(S.charAt(i), i);
foundQueue.remove(S.charAt(i));
foundQueue.add(S.charAt(i));
}
}
if (notFound.isEmpty()) {
if (minWindow == "")
minWindow = S.substring(leftIndex, i + 1);
else {
int windowLength = i - leftIndex;
if (windowLength < minWindow.length())
minWindow = S.substring(leftIndex, i + 1);
}
if (i < S.length() - 1) {
notFound.add(S.charAt(leftIndex));
found.remove(S.charAt(leftIndex));
foundQueue.remove(S.charAt(leftIndex));
if(!foundQueue.isEmpty())
leftIndex = found.get(foundQueue.peek());
}
}
}
return minWindow;
}
public static void main(String[] args) {
MinimumWindowSubstring mws = new MinimumWindowSubstring();
String S = "aa";
String T = "aa";
System.out.println(mws.minWindow(S, T));
}
}