diff --git a/solved/LeetCode/Challenges/2020/January/07.cc b/solved/LeetCode/Challenges/2020/January/07.cc new file mode 100644 index 00000000..d3ba3f8a --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/07.cc @@ -0,0 +1,26 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + map freq; + int i = 0; + int j = 0; + int best = 0; + while (i < s.size() && j < s.size()) { + while (j < s.size() && freq[s[j]] < 1) { + freq[s[j]]++; + j++; + best = max(best, j - i); + } + if (j >= s.size()) { + break; + } + while (i < s.size() && s[i] != s[j]) { + freq[s[i]]--; + i++; + } + freq[s[i]]--; + i++; + } + return best; + } +};