Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sourabh2k15/5bfefd3308d836272bc62860e3f44f3b to your computer and use it in GitHub Desktop.

Select an option

Save sourabh2k15/5bfefd3308d836272bc62860e3f44f3b to your computer and use it in GitHub Desktop.
Leetcode 3. Longest Substring Without Repeating Characters
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> seen;
int begin = 0, end = 0;
int len = 0;
string ans = "";
while(end < s.length()){
char current = s[end];
if(seen.count(current) == 1 && seen[current] >= begin){
begin = seen[current] + 1;
}
else{
seen[current] = end;
end++;
}
if(end - begin > len){
len = end - begin;
ans = s.substr(begin, end - begin);
}
}
return len;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment