Given two strings s and t of lengths m and n respectively, return the minimum window substring ofssuch that every character int(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.
A substring is a contiguous sequence of characters within the string.
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.
Constraints:
m == s.length
n == t.length
1 <= m, n <= 105
s and t consist of uppercase and lowercase English letters.
Follow up: Could you find an algorithm that runs in O(m + n) time?
ac1: two pointers
classSolution {publicStringminWindow(String s,String t) {// edge casesif (s ==null|| t ==null) return"";// record char and countsint[] note =newint[128];for (int i =0; i <t.length(); i++) { note[t.charAt(i)]++; }// walk original stringint f =0, cnt =t.length(), minlen =Integer.MAX_VALUE;for (int h =0, e =0; e <s.length(); e++) {// meet char, decrease 1 in noteif (note[s.charAt(e)]-->0) cnt--;// cnt == 0, find substring, do sthwhile (cnt ==0) {// record lengthif (minlen > e - h +1) { minlen = e - h +1; f = h; }// move f forward, slide windowif (h >= e) break; note[s.charAt(h)]++; if (note[s.charAt(h)] > 0) cnt++; // window lack one char, not qualified, move e forward to find a new window.
h++; } }// resultreturn minlen ==Integer.MAX_VALUE?"":s.substring(f, f + minlen); }}/*substring problem1. record char and countsint[] note = new int[128];walk through target string2. walk original string, meet char, decrease 1 in noteint f = 0, e = 0; // 2 pointersint cnt = t.length();when note[char] == 0, cnt--;3. if cnt == 0, find target substring in original string. record length4. move f forward, note[char]++*/