C++|Sliding Window | Hash Map |O(n)
Intuition\n Describe your first thoughts on how to solve this problem. \nIdea is to use a map and check when there is a duplicate remove it and maintain the cou
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIdea is to use a map and check when there is a duplicate remove it and maintain the counter for max window size.\nThis tempate ca be used for multiple questions like:\n- https://leetcode.com/problems/maximum-erasure-value/description/\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSo using Sliding Window These are the steps:\n\n- Insert one by one in map and also check a condition along with this .\n- Condition is if mp[s[i]] is >1 i.e is a duplicate so remove it .\n- Keep a maxi varaible for maximum window size.\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(128) --> can be taken as constant or surely less than using a map direclty\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int lengthOfLongestSubstring(string s) {\n      int n=s.size(),j=0,ans=0,maxi=0;\n      \n      vector<int>mp(128,0);\n        for(int i=0;i<n;i++){\n            if(!mp[s[i]]++)maxi++;\n            while(mp[s[i]]>1){\n                mp[s[j]]--;\n                j++;\n            }\n            ans=max(ans,i-j+1);\n        }\n        return ans;\n    \n    }\n};\n```\n# UpVote will Be  Apprecaited \uD83D\uDD3C\uD83D\uDD3C\uD83D\uDD3C
['Hash Table', 'String', 'Sliding Window', 'C++']