Skip to content

Commit

Permalink
[LeetCode] Add 07.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da authored Jan 7, 2021
1 parent ca1de7b commit 0d7d0a6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions solved/LeetCode/Challenges/2020/January/07.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char, int> 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;
}
};

0 comments on commit 0d7d0a6

Please sign in to comment.