Longest Substring Without Repeating Characters(C++)

Given a string, find the length of the longest substring without repeating characters.

class Solution {
public:
    int lengthOfLongestSubstring(string s) 
    {
        map<char,int> maps;
        int n=s.size();
        int index=0;
        int result=0;
        for(int i=0;i<n;i++)
        {
            if(maps.find(s[i])!=maps.end()&&maps[s[i]]>=index)
            {
                index=maps[s[i]]+1;
            }
            maps[s[i]]=i;
            result=max(result,i-index+1);
        }
        return result;
    }
};

猜你喜欢

转载自blog.csdn.net/zrh_csdn/article/details/80324255