My 10 lines O(N) precise and easy c++ code


  • 11
    G
      class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            vector<int> mymap(256,-1);
            int i,last=0,ans=0;
            for(i=0;i<s.length();i++) {
                if(mymap[s[i]]==-1 || mymap[s[i]]<last)
                    ans = max(ans,i-last+1);
                else
                    last = mymap[s[i]]+1;
                mymap[s[i]]=i;
            }
            return ans;
        }
    };

Log in to reply
 

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