Please help, oj showing wrong answer, while local machine giving right answer for test case "ab", "a"


  • 1
    S
    --pos for maintain all pos of all letters
    --index vector maintains index of letters present in T.
    
    
    
    #include<iostream>
    #include<list>
    #include<vector>
    #include<string>
    using namespace std;
    
    string minWindow(string s, string t) {
            int r=256;
         int *tcounts,  *scounts;
         tcounts=new int[r];
         scounts=new int[r];
         vector< list<int> > pos(r);
         for(int i=0;i<t.size();i++){
             tcounts[t[i] ]++;
         }
         list<int> index;int cover=0, rem=0;
         int minlen=s.length()+1, p, start, end;
         for(int i=0;i<s.size();i++){
             p=s[i];
             if( tcounts[p]  == 0)
                continue;
             else if( tcounts[ p] > scounts[ p ] ){
                 pos[p].push_back(i);
                 index.push_back(i);
                 scounts[p]++;
                 cover++;
                 if( cover == t.size()){
                   minlen=index.back()-index.front()+1;
                   start=index.front(), end=index.back();
                 }
             }else if( tcounts[p] == scounts[p]){
                 rem=pos[p].front();
                 pos[p].pop_front();
                 pos[p].push_back(i);
                 index.push_back(i);
                 if( index.front() == rem){
                     rem=index.front();
                     while(  rem != pos[s[rem]].front()){
                         index.pop_front();
                         rem=index.front();
                     }
                 }
                 
                 if( cover==t.size() && index.back()-index.front()+1 < minlen){
                     minlen=index.back()-index.front()+1;
                     start=index.front(), end=index.back();
                 }
                 
             }
         }
         if( minlen == s.length()+1) return "";
         return s.substr(start, end-start+1);
            
        }

  • 0
    K

    The problem seems arrays tcounts, scounts are not getting initialized to 0's.
    Try tcounts = new int[r]{0}; ...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.