lundi 26 juillet 2021

Can someone tell me the difference when i used

Can someone tell me the difference when i used i = a[s[j]] + 1; and i = max( i , a[s[j]] +1); Also according to me there is no use of giving max function. I GUESS

public:
    int lengthOfLongestSubstring(string s)
    {
        map<char,int> a;
        int len=0 , i = 0 , j = 0 ;
        int n = s.length();
        
        while(j<n)
        {
            if( a.find(s[j])!=a.end() )
                 i = max(i , a[s[j]] + 1);
            
            a[s[j]] = j;
            len = max(len , j - i + 1);
            j++;
        }
        
        return len;
    }
};
            if( a.find(s[j])!=a.end() )
                 i = a[s[j]] + 1;

Aucun commentaire:

Enregistrer un commentaire