-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminWinSub.java
More file actions
116 lines (89 loc) · 3.64 KB
/
minWinSub.java
File metadata and controls
116 lines (89 loc) · 3.64 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
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
import java.util.*;
class minWinSub {
public static String minWindow(String s, String t) {
if (s.length() < t.length()) return "";
// Freq map for t
Map<Character, Integer> tFreq = new HashMap<>();
for (char c : t.toCharArray()) {
tFreq.put(c, tFreq.getOrDefault(c, 0) +1);
}
// Slid win freq map
Map<Character, Integer> winFreq = new HashMap<>();
int left = 0;
int right = 0;
int minLen = Integer.MAX_VALUE;
int start = 0; // Start ind of min win
int formed = 0; // Count of unique chars in win matching required freq
// Num of unique chars in t that need to be matched
int required = tFreq.size();
// Expand the right bound of win
while (right < s.length()) {
char rightChar = s.charAt(right);
winFreq.put(rightChar, winFreq.getOrDefault(rightChar, 0) + 1);
// Check if the curr char in the win matches the freq
if (tFreq.containsKey(rightChar) && winFreq.get(rightChar).intValue() == tFreq.get(rightChar).intValue()) {
formed ++;
}
// Contract the win from left
while (left <= right && formed == required) {
char leftChar = s.charAt(left);
// Update the result if this win is smaller
if (right - left + 1 < minLen) {
minLen = right - left + 1;
start = left;
}
// Decrease freq of the left char
winFreq.put(leftChar, winFreq.get(leftChar) - 1);
if (tFreq.containsKey(leftChar) && winFreq.get(leftChar) < tFreq.get(leftChar)) {
formed --;
}
left ++;
}
// Expand the right
right ++;
}
// Return the min win or empty string if no win found
return minLen == Integer.MAX_VALUE ? "" : s.substring(start, start + minLen);
}
public static void main(String[] args) {
String s = "ADOBECODEBANC";
String t = "ABC";
System.out.println(minWindow(s, t));
}
}
/*
sliding win:
Use a map to store the frequency of each char in t
tFreq will help determin if a substring s contains all of t
2 Pointers:
left, right for win in s
expand right, add chars to winFreq map that tracks chars in a current win
Win is valid check:
valid if all chars in t are present with the rquired freq
maintain a formed counter that increments when a char in the win reaches the req'd freq in tFreq and decrements when it falls below
Minimize the win:
when valid win(formed = num of unique chars in t), shrink the win bymoving in left
keep track of the smallest win
Return result:
return substr using the recorded start position and length
Time complex: O(m+n)
Space complex: O(n+m)
https://leetcode.com/problems/minimum-window-substring/description/
Given two strings s and t of lengths m and n respectively, return the minimum window
substring
of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".
The testcases will be generated such that the answer is unique.
Example 1:
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Example 2:
Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.
Example 3:
Input: s = "a", t = "aa"
Output: ""
Explanation: Both 'a's from t must be included in the window.
Since the largest window of s only has one 'a', return empty string.
*/