1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public: int lengthOfLongestSubstring(string s) { unordered_set<char> cur_group; int end = 0; while (end < s.size() && cur_group.find(s[end]) == cur_group.end()) { cur_group.insert(s[end++]); } unsigned long max_length = cur_group.size(); for (int i = 0; i < s.size(); i++) { cur_group.erase(s[i]); while (end < s.size() && cur_group.find(s[end]) == cur_group.end()) { cur_group.insert(s[end++]); } max_length = std::max(max_length, cur_group.size()); } return max_length; } };
|