Minimum Window Substring
约 1 分钟· 本文阅读 次
本页目录
Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is BANC.
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
Solution & Complexity
- Use two pointers: start and end. First, move 'end'. After finding all the needed characters, move 'start'. O(n)
- Use array as hashtable.
Code
public class Solution {
public String minWindow(String S, String T) {
int N = S.length(), M = T.length();
if (N < M) return new String("");
int[] need = new int[256];
int[] find = new int[256];
for (int i = 0; i < M; ++i)
need[T.charAt(i)]++;
int count = 0, resStart = -1, resEnd = N;
for (int start = 0, end = 0; end < N; ++end)
{
if (need[S.charAt(end)] == 0)
continue;
if (find[S.charAt(end)] < need[S.charAt(end)])
count++;
find[S.charAt(end)]++;
if (count != M) continue;
// move 'start'
for (; start < end; ++start) {
if (need[S.charAt(start)] == 0) continue;
if (find[S.charAt(start)] <= need[S.charAt(start)]) break;
find[S.charAt(start)]--;
}
// update result
if (end - start < resEnd - resStart) {
resStart = start;
resEnd = end;
}
}
return (resStart == -1) ? new String("") : S.substring(resStart, resEnd + 1);
}
}相关文章
- All Permutations II2016-01-10
- All Permutation2016-01-10
- All Subsets2016-01-10
- Best Time to Buy and Sell Stock II2016-01-10
已恢复到上次阅读位置