题目

July 7, 2021 · View on GitHub

请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。

假设字符串中只包含从 a 到 z 的字符。

样例

输入:"abcabc"

输出:3

参考答案

class Solution {
public:
    int longestSubstringWithoutDuplication(string s) {
        unordered_map<char,int> hash;
        int res=0;
        for(int i=0,j=0;j<s.size();j++)
        {
            hash[s[j]]++;
            while(hash[s[j]]>1)
            {
                hash[s[i++]]--;
            }

            res=max(res,j-i+1);
        }
        return res;
    }
};