TLE in Length Of Longest Substring Without REpeated Characters


  • 0
    M
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) 
        {
            unordered_map<char,int> m;
            int len = 0, mx =0;
            
            for(int i=0;i<s.length();)
            {
                if(m.find(s[i])==m.end()) 
                {
                    m.emplace(s[i],i);
                    i++; len++;
                    if(len>mx) mx=len;
                    continue;
                }
                
                else
                { 
                    i = m.find(s[i])->second+1;
                    len =0;
                    m.clear();
                    
                }
                
            }
                
            return mx;
            
            
        }
            
            
            
            
            
            
        
    };

Log in to reply
 

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