1. 程式人生 > >[ZZ]最小覆蓋子串演算法

[ZZ]最小覆蓋子串演算法

題目:

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 emtpy string “”.

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

題意就是在S中選一個最小的子串,使得包含T中所有的字母。
思路:
i,j表示S串的起點與終點;
一個數組儲存 int count[255] T串在s中的字母出現的次數;
一個佇列儲存字母出現的順序;
minValue儲存最小的長度;
num 表示在i,j 之間的有效個數;

思路:滑動i,j直到num =s的長度,然後根據佇列中的值去移動i,和更新count ,num ,minvalue