Longest Substring Without Repeating Characters LeetCode solution in C++

1
class Solution {
2
public:
3
int lengthOfLongestSubstring(string s) {
4
map<char, int> mp;
5
int ans = 1;
6
for (auto ch : s) {
7
if (mp.find(ch) != mp.end()) {
8
while (mp.find(ch) != mp.end()) mp.erase(mp.begin());
9
}
10
mp.insert({ch, 1});
11
if (mp.size() > ans) ans = mp.size();
12
}
13
return ans;
14
}
15
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0