Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
int lengthOfLongestSubstring(char* s) {
int latestPos[]; //map<char,int>
int result = ;
int i = ;
int startPos = ; if(strcmp(s,"")==) return result; memset(latestPos, -, sizeof(latestPos));
latestPos[s[]] = ;
while(s[i]!='\0'){
if(latestPos[s[i]] >= startPos){ //s[i] already appeared
if(i-startPos > result) result = i-startPos;
startPos = latestPos[s[i]]+;
}
latestPos[s[i]] = i;
i++;
}
if(i-startPos > result) result = i-startPos;
return result;
}