$\textrm{Problem Description : }$$\href{https://leetcode.cn/problems/longest-substring-without-repeating-characters/}{\textrm{LeetCode3-无重复字符的最长子串}}$

求字符串中不含重复字符的最长子串的长度

$\textrm{Solution:}$

滑动窗口:当不含重复字符时,窗口右边界滑动,更新答案; 含重复字符时,左滑窗口,直至不含重复字符。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        bool vis[256] = {false};
        int left = 0,right,ans = 0;
        for(right = 0;right < s.size();right++)
        {
            while(vis[s[right]])
                vis[s[left++]] = false;
            vis[s[right]] = true;
            ans = max(ans,right-left+1);
        }
        return ans;
    }
};

$\textrm{Author}$@$\href{http://kuroko.info}{\textrm{Kuroko}}$

$\textrm{GitHub}$@$\href{https://github.com/SuperKuroko}{\textrm{SuperKuroko}}$

$\textrm{LeetCode}$@$\href{https://leetcode-cn.com/u/kuroko177/}{\textrm{kuroko177}}$

$\textrm{Last Modified: 2023-02-17 16:18}$