Created
January 8, 2018 23:10
-
-
Save sourabh2k15/5bfefd3308d836272bc62860e3f44f3b to your computer and use it in GitHub Desktop.
Leetcode 3. Longest Substring Without Repeating Characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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