Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 890 Bytes

17.md

File metadata and controls

36 lines (28 loc) · 890 Bytes

Method 1

int maxLengthBetweenEqualCharacters(string s) {
        
        unordered_map<char,int>m;
        int maxi=-1;

        for(int i=0;i<s.size();i++)
        {
            if(m.find(s[i])!=m.end())
            {
                maxi=max(maxi,i-m[s[i]]-1);
            }
            else    
                m[s[i]]=i;
        }
        return maxi;
    }

Method 2

 int maxLengthBetweenEqualCharacters(string s) {
        int ans = -1;
        for (int left = 0; left < s.size(); left++) 
            for (int right = left + 1; right < s.size(); right++) 
                if (s[left] == s[right]) 
                    ans = max(ans, right - left - 1);
                
            
        }