请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
假设字符串中只包含从’a’到’z’的字符。
样例
输入:“abcabc”
输出:3
思路:
双指针
class Solution {
public:
int longestSubstringWithoutDuplication(string s) {
vector<int>vis(30,0);
int n = s.size();
if(n == 0) return 0;
int sta = 0,ed = 0;
vis[s[0] - 'a'] = 1;
int ans = 0;
while(ed < n) {
ans = max(ed - sta + 1,ans);
if(!vis[s[ed + 1] - 'a']) ed++,vis[s[ed] - 'a']++;
else {
vis[s[sta] - 'a']--;sta++;
}
}
return ans;
}
};