From 0d7d0a699480c5ed827e36d584afc736cff66dfd Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Thu, 7 Jan 2021 16:15:18 +0100 Subject: [PATCH] [LeetCode] Add 07.cc --- solved/LeetCode/Challenges/2020/January/07.cc | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solved/LeetCode/Challenges/2020/January/07.cc 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; + } +};