diff --git a/3_longest_substring_without_repeating_characters/main.cpp b/3_longest_substring_without_repeating_characters/main.cpp new file mode 100644 index 0000000..41a6839 --- /dev/null +++ b/3_longest_substring_without_repeating_characters/main.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int maxLen = 0, currentLen = 0; + queue substr; + unordered_set charset; + + for (int index = 0; index < s.length(); index++) { + char currentChar = s[index]; + + // has repeat + if (charset.find(currentChar) != charset.end()) { + while (substr.front() != currentChar) { + charset.erase(substr.front()); + substr.pop(); + currentLen--; + } + charset.erase(currentChar); + substr.pop(); + currentLen--; + } + + charset.emplace(currentChar); + substr.emplace(currentChar); + currentLen++; + + maxLen = max(maxLen, currentLen); + } + + return maxLen; + } +};