Simple c++ solution


  • 0
    L
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            vector<int> dict(255,-1);//a hash map
            int longest=0;
            int ilong=0;//longest non-repeating substring terminated by s[i]
            for(int i=0;i<s.length();i++){
                if (dict[s[i]]<i-ilong){//out of valid hash map bounds, treat dict[s[i]] as never seen.
                    ilong++;
                } else {
                    ilong=i-dict[s[i]];
                }
                longest=max(ilong,longest);
                dict[s[i]]=i;
            }
            
            return longest;
        }
    };

Log in to reply
 

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