[LeetCode] Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.

For example, given:
S"barfoothefoobarman"
L["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

这题还是挺复杂的,最主要设计到两大块代码,一个是hash字符串,还有一个是找最小窗口。可以做到O(n)吧,n = s.length()

  1 class Solution {
  2 private:
  3     int count[1000];
  4     int countSize;
  5     map<string, int> index;
  6     vector<int> ret;
  7 public:
  8     vector<int> findSubstring(string S, vector<string> &L) {
  9         // Start typing your C/C++ solution below
 10         // DO NOT write int main() function
 11         ret.clear();
 12         if (L.size() == 0)
 13             return ret;
 14 
 15         index.clear();
 16         countSize = 0;
 17         for(int i = 0; i < L.size(); i++)
 18             if (index.count(L[i]) > 0)
 19                 count[index[L[i]]]++;
 20             else
 21             {
 22                 index[L[i]] = countSize;
 23                 count[countSize++] = 1;
 24             }
 25 
 26             int step = L[0].size();
 27 
 28             vector<int> a;
 29 
 30             for(int i = 0; i < step; i++)
 31             {
 32                 a.clear();
 33                 for(int j = i; j < S.size(); j += step)
 34                 {
 35                     if (j + step <= S.size())
 36                     {
 37                         string s(S, j, step);
 38                         if (index.count(s) > 0)
 39                             a.push_back(index[s]);
 40                         else
 41                             a.push_back(-1);
 42                     }
 43                 }
 44 
 45                 int beg = -1;
 46                 int end = 0;
 47                 int size = L.size();
 48                 while(end < a.size())
 49                 {
 50                     if (a[end] != -1)
 51                     {
 52                         if (count[a[end]] > 0)
 53                         {
 54                             if (beg == -1)
 55                                 beg = end;
 56                             size--;
 57                             count[a[end]]--;
 58                         }
 59                         else
 60                         {
 61                             while(beg < end)
 62                             {
 63                                 count[a[beg]]++;
 64                                 size++;
 65                                 if (a[beg++] == a[end])
 66                                     break;
 67                             }
 68                             count[a[end]]--;
 69                             size--;
 70                         }
 71                     }
 72                     else
 73                     {
 74                         size = L.size();
 75                         if (beg != -1)
 76                         {    
 77                             for(int i = beg; i < end; i++)
 78                                 count[a[i]]++;
 79                         }
 80                         beg    = -1;
 81                     }
 82 
 83                     end++;
 84 
 85                     if (size == 0)
 86                     {
 87                         ret.push_back(beg * step + i);
 88                         size++;
 89                         count[a[beg]]++;
 90                         beg++;
 91                     }
 92                 }
 93 
 94                 if (beg != -1)
 95                 {
 96                     for(int i = beg; i < end; i++)
 97                         count[a[i]]++;
 98                 }
 99             }
100 
101             return ret;
102     }
103 };
posted @ 2012-11-19 16:57  chkkch  阅读(1469)  评论(1编辑  收藏  举报