Clean c++ ac code, o(n) runtime


  • 4
    P
      int lengthOfLongestSubstring(string s) {
                int result(0),current(0);
                unordered_map<char,int> hash;
                for(int i=0;i<s.size();i++){
                    if(hash.find(s[i]) != hash.end()){
                        result = max(result,current);
                        current = min(i-hash[s[i]],current+1);
                    }
                    else{
                        current++;
                    }
                    hash[s[i]] = i;
                }
                return max(result,current);
            }

Log in to reply
 

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