Created
August 22, 2016 20:51
-
-
Save mgild/524e52d0f433f000157b6253992cd360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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