Skip to content

Instantly share code, notes, and snippets.

@mgild
Created August 22, 2016 20:51
Show Gist options
  • Save mgild/524e52d0f433f000157b6253992cd360 to your computer and use it in GitHub Desktop.
Save mgild/524e52d0f433f000157b6253992cd360 to your computer and use it in GitHub Desktop.
int Solution::lengthOfLongestSubstring(string A) {
size_t longest = 0;
// holds the characters of the current substring
unordered_set<char> currSet;
for (int i = 0; i < A.size(); ++i) {
// if the current character already exists in the substring
if (!currSet.insert(A[i]).second) {
longest = max(longest, currSet.size());
// erase the most senior elements of the set until insertion succeeds
while (!currSet.insert(A[i]).second) {
currSet.erase(A[i - currSet.size()]);
}
}
}
return max(longest, currSet.size());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment