leetcode Substring with Concatenation of All Words

class Solution {
public:
    vector<int> findSubstring(string S, vector<string> &L) 
    {
        vector<int>v;
        if(L.size()==0)return v;        
        int length=L[0].size();   
        map<string,int>m;
        for(int i=0;i<L.size();i++)
        m[L[i]]++;
        map<string,int>m1;
        for(int i=0;i+L.size()*length<=S.size();i++)
        {          
           m1.clear();
           map<string,int>::iterator it;
           int j,k;
           for(j=i,k=0;k<L.size();j=j+length,k++)
           {
               string comp=S.substr(j,length);
               it=m.find(comp);
               if(it==m.end())
               break;          
               m1[comp]++;
               if(m1[comp]>m[comp])break;
           }       
           if(k==L.size())
           {
             v.push_back(i);          
           }          
        }        
        return v;       
    }  
};

  

posted @ 2013-06-26 21:52  代码改变未来  阅读(908)  评论(0编辑  收藏  举报