[Java/C++] A reall Detailed Explanation
So, the prerequisit of this problem is Sliding Window, if you know then it\'s a plus point. But, if you don\'t know don\'t worry I\'ll try to teach you.\n\nLet\
So, the prerequisit of this problem is **Sliding Window**, if you know then it\'s a plus point. But, if you don\'t know don\'t worry I\'ll try to teach you.\n\nLet\'s understand first of all what the problem is saying!!\n```\nGiven a string s, find the length of the longest substring without repeating characters.\n```\nOkay, so from the given statement we will try to find out wether it is a **Sliding Window** problem or not>>\n\nSo, to check that out I\'m giving you a tempelate & it\'ll work in almost all of the questions of **sliding window**\n\n```\nTo, find out a sliding window problem :-\n> First thing is, we have given something like an "Array" | OR | "String"\n> Second thing is, they are talking about either "subsequence" | OR | "substring"\n> And third most thing is, either we have given a "window size i.e. k" | OR | we have to "manually find out window size" \n```\n\nNow, using above keys let\'s understand wether this problem is of a sliding window or not.\n```\n> Are they talking about, "Array" or "String" --> yes they are talking about "string" +1 point\n> Are they asking to find out "subsequence" or "substring" --> yes they are talking about "substring" +1 point\n> Do, we have given a window size --> No, we don\'t have\n\nTotal score is "2 / 3" so, it\'s a 100% sliding window problem. If your score lies from 2/3 to 3/3 that\'s a gauranteed sliding window problem \n```\n\nNow, let\'s talk about how we gonna implement sliding window in this problem, but before that I just want to tell you one more thing. There\'s exist basically 2 types of sliding window.\n1. Fix size sliding window **{means K is given}**\n\n\n2. Variable silze sliding window **{means K is not given}**\n\n**Before moving to the problem I want to give you a template which you can use in any sliding window `{Variable size}` problem**\n\n```\nwhile(j < size()){\n\n    // Calculation\'s happen\'s here\n-----------------------------------------------\n    if(condition < k){\n        j++;\n    }\n-----------------------------------------------\n\n-----------------------------------------------\n    else if(condition == k){\n        // ans <-- calculation\n        j++;\n    }\n----------------------------------------------\n\n----------------------------------------------\n    else if(condition > k){\n        while(condition > k){\n            // remove calculation for i\n            i++;\n        }\n        j++;\n    }\n----------------------------------------------\n}\nreturn ans;\n```\n\nSo, in this problem we gonna deal with variable size sliding window. Let\'s take one example :-\n\n```\nInput: s = "abcabcbb"\nOutput: 3\n```\n\nSo, inorder to solve this, what I\'m thinking is, we should have to use one more Data Structure to store the occurence of these characters, I thing HashMap will be best to use.\n\n* Now, what I\'ll do is create 2 pointer\'s i & j initally they both start from 0\n* The j pointer will helps us to fill the array while the i pointer will helps in removing from the map {Don\'t know what I\'m talking about} just wait. You\'ll understand :)\n\n```\nLet\'s understand it visually :-\n```\n\n![image](https://assets.leetcode.com/users/images/664cc9c6-1440-4b88-a11f-ca27a67b5266_1654838647.047003.png)\n\n![image](https://assets.leetcode.com/users/images/088afd9c-bc21-46ca-b651-a46a75c02c4e_1654838698.0517573.png)\n\n![image](https://assets.leetcode.com/users/images/61717331-f54a-4940-9000-2bf95dc8af56_1654838746.162897.png)\n\n![image](https://assets.leetcode.com/users/images/3c62fa7f-e7e3-4063-b306-0a3495071004_1654838786.1087303.png)\n\n![image](https://assets.leetcode.com/users/images/c1aae088-b5e1-4328-aa1f-af414b0ef112_1654838837.5751796.png)\n\n![image](https://assets.leetcode.com/users/images/fe893a02-e560-4a8f-aaa1-f92e55dd35cd_1654838885.7713046.png)\n\n![image](https://assets.leetcode.com/users/images/6a268e77-c099-4f01-b0d5-136995310d3f_1654838919.9853337.png)\n\n![image](https://assets.leetcode.com/users/images/6811ecde-9e2a-47e6-b688-682bb3f0f5a2_1654838956.3712077.png)\n\n![image](https://assets.leetcode.com/users/images/a751f95a-f42b-4922-9609-c88ad5f339fb_1654838991.3203616.png)\n\n![image](https://assets.leetcode.com/users/images/f789c5cb-3e73-449e-80de-38f61d5404b9_1654839058.5881085.png)\n\n![image](https://assets.leetcode.com/users/images/65c28458-d89a-4008-afde-264598e4d127_1654839089.2232268.png)\n\n![image](https://assets.leetcode.com/users/images/2fb45c3b-a510-4a3b-be1d-02a53fc0ff02_1654839123.2810624.png)\n\n![image](https://assets.leetcode.com/users/images/5c80c220-70d8-4a9e-9689-a7cd1fb9e11d_1654839162.3931215.png)\n\n![image](https://assets.leetcode.com/users/images/b10fc6ab-9f8b-4035-bfbf-5b9e4cca3cb0_1654839175.0759387.png)\n\n\n**Java**\n```\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        Map<Character, Integer> map = new HashMap<>();\n        int i = 0;\n        int j = 0;\n        int max = 0;\n        while(j < s.length()){\n            map.put(s.charAt(j), map.getOrDefault(s.charAt(j), 0) + 1);\n            if(map.size() == j - i + 1){\n                max = Math.max(max, j - i + 1);\n                j++;\n            }\n            else if(map.size() < j - i + 1){\n                while(map.size() < j - i + 1){\n                    map.put(s.charAt(i), map.get(s.charAt(i)) - 1);\n                    if(map.get(s.charAt(i)) == 0) map.remove(s.charAt(i));\n                    i++;\n                }\n                j++;\n            }\n        }\n        return max;\n    }\n}\n```\n**C++**\n```\nclass Solution {\npublic:\n    int lengthOfLongestSubstring(string s) {\n        if(s.length()==0)return 0;   //if string of length zero comes simply return 0\n        unordered_map<char,int> m;   //create map to store frequency,(get to know all unique characters\n        int i=0,j=0,ans=INT_MIN; \n        while(j<s.length())   \n        {\n            m[s[j]]++;  //increase the frequency of the element as you traverse the string\n            if(m.size()==j-i+1)  // whem map size is equal to the window size means suppose window size is 3 and map size is also three that means in map all unique characters are their\n            {\n                ans = max(ans,j-i+1);  //compare the length of the maximum window size\n            }\n            else if(m.size()<j-i+1)   //if the map size is less than the window size means there is some duplicate present like window size = 3 and map size = 2 means there is a duplicates\n            {\n                while(m.size()<j-i+1)  //so till the duplicates are removed completely\n                {\n                    m[s[i]]--;   //remove the duplicates\n                    if(m[s[i]]==0)  //if the frequency becomes zero \n                    {\n                        m.erase(s[i]);//delete it completely\n                    }\n                    i++;  //go for next element \n                }\n            }\n             j++;  //go for the next element\n        }\n        return ans;\n    }\n};\n```
['C', 'Java']