O(n) Solution in C++ || Sliding Window || HashMaps
The idea here is pretty simple... we use a map to keep track of already visited characters...\nWe initialize another pointer (j) which gives us the starting ind
The idea here is pretty simple... we use a map to keep track of already visited characters...\nWe initialize another pointer (j) which gives us the starting index of current window and we traverse through the string using pointer i, which indicated current index of our window...\n* If the character at ith index is not present in our map, we simply add it to our map and continue the iteration...\n* If the character at ith index is already present, but its index does not lie in our window, it implie that current character is not part of our current string and hence we update its index value in our map and continue with the iteration...\n* If the character at ith index is already present and the index at which it is present lies in our current window, then we update the starting index of current window as i value and our window is now a new window with size 1...\n\nWe keep track of our longest length using res variable in each iteration...\n\n*Here amortized TC is O(n), considering map searching is O(1), to get actual O(1) searching TC, we can use vector of  size 128 to store our characters...*\n\n**Pls upvote this thread if you found the solution helpful**\n\n\n```\nclass Solution {\npublic:\n    int lengthOfLongestSubstring(string s) {\n        if(s.size()<=1) return s.size();\n        \n        map<char, int> mp;\n        int curr=0, res=0;\n        for(int i=0;i<s.size();i++) {\n\t\t\t//i-curr, gives us the count of characters in our current window (curr is starting of our window)\n            res=max(res, i-curr);\n\t\t\t\n\t\t\t//If the character is present and its index is present in current window, we update our current window...\n            if(mp.find(s[i])!=mp.end() && mp[s[i]]>=curr) {\n                curr = mp[s[i]]+1;\n            }\n            mp[s[i]] = i;\n        }\n\t\t\n\t\t//This is to handle the case when the longest substring is at the end our string, in this case we wont enter the if condition and hence curr variable won\'t be updated...\n        if(res<s.size()-curr) return s.size()-curr;\n\t\t\n        return res;\n    }\n```
['C', 'Sliding Window', 'C++']