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