Minimum Window Substring

2014.2.26 21:17

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.

Solution:

  First of all, it's important to understand the definition of "window substring".

  For two strings s1 and s2, if ss is a substring of s1, and you can pick out some letters from ss to form s2, ss is a valid window substring of s2 in s1.

  A window substring may contain redundant letters. That's why you have to find the "minimum", that contains the least number of redundant letters.

  My solution is linear and goes from left to right. A simple hash table is used to count the occurence of all letters. Here the letters are limited to ASCII characters only.

  Total time complexity is O(n). Space complexity is O(1).

Accepted code:

 1 // 4CE, 5WA, 1AC, O(n) solution, mein Gott (O_o)(>_<)(u_u)(x_X)
 2 class Solution {
 3 public:
 4     string minWindow(string S, string T) {
 5         int i;
 6         int tc[256];
 7         int c[256];
 8         int cc;
 9         int slen, tlen;
10         int ll, rr;
11         int mll, mrr;
12         
13         slen = (int)S.length();
14         tlen = (int)T.length();
15         if (slen == 0 || tlen == 0 || slen < tlen) {
16             return "";
17         }
18         
19         for (i = 0; i < 256; ++i) {
20             c[i] = 0;
21             tc[i] = 0;
22         }
23         
24         for (i = 0; i < tlen; ++i) {
25             ++tc[T[i]];
26         }
27         cc = 0;
28         
29         mll = -1;
30         mrr = -1;
31         ll = 0;
32         while (ll < slen && tc[S[ll]] == 0) {
33             // skip irrelavant letters
34             ++ll;
35         }
36         if (ll == slen) {
37             // S and T have no letters in common
38             return "";
39         }
40         
41         rr = ll;
42         while (rr < slen) {
43             ++c[S[rr]];
44             if (c[S[rr]] <= tc[S[rr]]) {
45                 // S = caae, T = cae
46                 // the window may contain redundant letters in T
47                 ++cc;
48             }
49             
50             while (cc == tlen) {
51                 if (mll == -1 || (rr - ll + 1) < (mrr - mll + 1)) {
52                     // a better result is found
53                     mll = ll;
54                     mrr = rr;
55                 }
56                 // move ll to right
57                 --c[S[ll]];
58                 if (c[S[ll]] < tc[S[ll]]) {
59                     --cc;
60                 }
61                 ++ll;
62                 
63                 while (ll < slen && tc[S[ll]] == 0) {
64                     // skip irrelavant letters
65                     ++ll;
66                 }
67                 if (rr < ll) {
68                     // ll must not go over rr
69                     rr = ll;
70                 }
71             }
72             ++rr;
73             while (rr < slen && tc[S[rr]] == 0) {
74                 ++rr;
75             }
76         }
77         
78         if (mll == -1) {
79             return "";
80         } else {
81             return S.substr(mll, mrr - mll + 1);
82         }
83     }
84 };

 

 posted on 2014-02-26 21:26  zhuli19901106  阅读(239)  评论(0编辑  收藏  举报