C++ | O(N) | Using Frequency Map |
Use Sliding Window approach using frequency map.\n As soon as frequency of any character becomes more than one, contract window till frequency of that character
* Use Sliding Window approach using frequency map.\n* As soon as frequency of any character becomes more than one, contract window till frequency of that character reduces to 1.\n* Window is marked by `st` and `end` index.\n* Update `ans` at each `end` index.\n* **TC : O(N)**\n```\nclass Solution {\npublic:        \n    int lengthOfLongestSubstring(string s) {        \n        unordered_map<char, int> freq;        \n        int n = s.length(), st = 0, end = 0, ans = 0;\t\t\n        while(end < n){            \n            freq[s[end]]++;            \n            while(freq[s[end]] != 1){\n                freq[s[st]]--;\n                st++;\n            }            \n            ans = max(ans, end - st + 1);\n            end++;                   \n        }        \n        return ans;        \n    }\n};\n```
['C', 'Sliding Window']