C++ | Short & Easy Solutions ✅
Please upvote if you like the solution & post :)\n\nSliding-window + Unordered_set Solution:\n\nclass Solution {\npublic:\n\tint lengthOfLongestSubstring(string
**`Please upvote if you like the solution & post :)`**\n\n**`Sliding-window + Unordered_set` Solution:**\n```\nclass Solution {\npublic:\n\tint lengthOfLongestSubstring(string s) {\n\t\tunordered_set<char> set;\n\t\tint i = 0, j = 0, n = s.size(), ans = 0;\n\t\twhile(j<n){\n\t\t\tif(set.find(s[j]) == set.end()){ //If the character does not in the set\n\t\t\t\tset.insert(s[j++]); //Insert the character in set and update the j counter\n\t\t\t\tans = max(ans, j-i); //Check if the new distance is longer than the current answer\n\t\t\t}\n\t\t\telse{\n\t\t\t\t//If character does exist in the set, ie. it is a repeated character, \n\t\t\t\t//we update the left side counter i, and continue with the checking for substring. \n\t\t\t\tset.erase(s[i++]); \n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}\n};\n```\n\n**Time Complexity :** `O(n)`\n**Space Complexity :** `O(min(a,b))` for the `unordered_set`, `a` is the upper bound & is the `size` of the string while `b` is the size of number of `characters` in the character set\n\n****\n**`Using Frequency Array` Solution:**\n```\nint lengthOfLongestSubstring(string s) {\n        int freq[256]={0};\n        int l=0,r=0,ans=0,n=s.size();\n        while(r<n){\n            freq[s[r]]++;\n            while(freq[s[r]]>1){\n                freq[s[l]]--;\n                l++;\n            }\n            ans = max(ans,r-l+1);\n            r++;\n        }\n        return ans;\n    }\n```\n**Time Complexity :** `O(n)`\n**Space Complexity :** `O(n)`\n****
['C', 'Sliding Window']