Longest Substring without Repeating Characters


  • 0
    I
    #include <unordered_map>
    
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            unordered_map <char, int> dict;
            unordered_map <char, int> :: iterator it;
            int maxLen = 0;
            char letter;
            int endLen;
            
            for(int i = 0, j = 0; j < s.length(); ++j){
                letter = s.at(j);
                it = dict.find(letter);
                if(it != dict.end()){
                    i = it -> second;
                    maxLen = max(maxLen, j - i);
                    it -> second = j;
                } else {
                    dict.insert(std::make_pair(letter, j));
                }
            }
            
            return maxLen;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.