Longest Substring Without Duplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| using namespace std;
string longestSubstringWithoutDuplication(string str) { int pos = -1, len = -1, n = str.length(), counter = 0, l = 0, r = 0; unordered_map<char, int> mp; while(r < n) { if(++mp[str[r++]] == 2) counter++; while(counter) { if(--mp[str[l++]] == 1) counter--; } if(len < (r - l)) { len = r - l; pos = l; } } return str.substr(pos, len); }
|