无重复字符最长字符串

3. 无重复字符的最长子串 - 力扣(LeetCode)

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
      std::map<char, int> cnt;
      int left = 0, ans = 0;
      for (int right = 0; right < s.size(); right++) {
        cnt[s[right]]++;
        while (cnt[s[right]] > 1) {
          cnt[s[left]]--;
          left++;
        }
        ans = std::max(ans, right - left + 1);
      }
      return ans;
    }
};