问题
代码
//C++
class Solution {
public:
int longestSubstringWithoutDuplication(string s) {
int st[26] = {0};
int res = 0;
for (int i = 0, j = 0; i < s.size(); i++)
{
st[s[i] - 'a']++;
while (j < i && st[s[i] - 'a'] > 1)
{
st[s[j] - 'a']--;
j++;
}
res = max(res, i - j + 1);
}
return res;
}
};
//C
int longestSubstringWithoutDuplication(char* s) {
int A[26] = {0};
int res = 0;
int i,j;
for(i=0,j=0;j<strlen(s);j++){
A[s[j]-'a']++;
while(i<j && A[s[j]-'a'] > 1){
A[s[i]-'a']--;
i++:
}
res=res>(j-i+1)?res:(j-i+1);
}
return res;
}