[Dynamic Size Sliding Window Tutorial] Longest Substring Without Repeating Characters
Topic :  Sliding Window\nSliding Window problems are problems in which a fixed or variable-size window is moved through a data structure, typically an array or 
**Topic** :  Sliding Window\nSliding Window problems are problems in which a fixed or variable-size window is moved through a data structure, typically an array or string, to solve problems efficiently based on continuous subsets of elements. This technique is used when we need to find subarrays or substrings according to a given set of conditions. (GFG Reference)\n\nThere are two types of sliding window:\n- Fixed Size Sliding Window\n- Dynamic Size Sliding Window\n\n___\n\n## Dynamic Size Sliding Window\nA **Dynamic Size Sliding Window** refers to a window or subarray whose size is not fixed; instead, it dynamically expands or shrinks based on whether it meets specified conditions.\n\n## Intuition Behind Dynamic Size Sliding Window\nGenerally, questions involving sliding window techniques often inquire about the number of subarrays or substrings that satisfy specific conditions.\n\nBy using brute force, we enumerate all subarrays or substrings and check whether each satisfies the condition. This enumeration typically results in at least `O(n^2)` time complexity.\n\nFor a fixed variable `L`, we check all subarrays `[L : R]` where `L \u2264 R`. The sliding window technique can be applied when all subarrays `[L : M]` satisfy the condition, and all subarrays with a left bound \'L\' and any right bound `[M + 1 : R]` fail the condition.\n\nLet\'s assume our current window has the range `[L : R]`. If the subarray `[L : R + 1]` satisfies the condition, we can continue expanding the right pointer `R` to find the maximum `M` for the current fixed `L`. If `[L : R + 1]` fails the condition, there is no need to check `[L : R + n]` because all subsequent subarrays will also fail the condition. Now it is time to expand the left pointer.\n\n## Solution\nFirst, it\'s essential to determine whether the sliding window technique is applicable. This method is suitable when:\n```\nThe sliding window technique can be applied when all subarrays `[L : M]` satisfy the condition, and all subarrays with a left bound \'L\' and any right bound `[M + 1 : R]` fail the condition.\n```\nThis question satisfies these criteria, making it suitable for applying the sliding window technique.\n\nWe initialize a variable `count` to keep track of the frequency of each character within our window. We use a for-loop to expand the index `i` as the right pointer while it satisfies the condition. If `count[s[i]] > 1`, it indicates that the current window fails the condition, and we need to expand the left pointer. While moving the pointers, we continuously update the frequency of each character and our answer as necessary.\n\n\n\n<br/><br/>\n\n\n```c++ []\nclass Solution {\npublic:\n    int lengthOfLongestSubstring(string s) {\n        int ans = 0;\n        vector<int> count(256);\n        for(int i = 0, j = 0; i < s.size(); i++) {\n            count[s[i]]++;\n            while(count[s[i]] > 1) {\n                count[s[j++]]--;\n            }\n            ans = max(ans, i - j + 1);\n        }\n        return ans;\n    }\n};\n```\n```java []\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        int ans = 0;\n        int[] count = new int[256];\n        for (int i = 0, j = 0; i < s.length(); i++) {\n            count[s.charAt(i)]++;\n            while (count[s.charAt(i)] > 1) {\n                count[s.charAt(j++)]--;\n            }\n            ans = Math.max(ans, i - j + 1);\n        }\n        return ans;\n    }\n}\n```\n```python []\nclass Solution:\n    def lengthOfLongestSubstring(self, s: str) -> int:\n        ans = 0\n        count = [0] * 256\n        j = 0\n        for i in range(len(s)):\n            count[ord(s[i])] += 1\n            while count[ord(s[i])] > 1:\n                count[ord(s[j])] -= 1\n                j += 1\n            ans = max(ans, i - j + 1)\n        return ans\n```\n```Go []\nfunc lengthOfLongestSubstring(s string) int {\n    ans := 0\n    count := make([]int, 256)\n    for i, j := 0, 0; i < len(s); i++ {\n        count[s[i]]++\n        for count[s[i]] > 1 {\n            count[s[j]]--\n            j++\n        }\n        if i-j+1 > ans {\n            ans = i - j + 1\n        }\n    }\n    return ans\n}\n```\n```PHP []\nclass Solution {\n\n    /**\n     * @param String $s\n     * @return Integer\n     */\n    function lengthOfLongestSubstring($s) {\n        $ans = 0;\n        $count = array_fill(0, 256, 0);\n        $j = 0;\n        for ($i = 0; $i < strlen($s); $i++) {\n            $count[ord($s[$i])]++;\n            while ($count[ord($s[$i])] > 1) {\n                $count[ord($s[$j++])]--;\n            }\n            $ans = max($ans, $i - $j + 1);\n        }\n        return $ans;\n    }\n}\n```\n```javascript []\n/**\n * @param {string} s\n * @return {number}\n */\nvar lengthOfLongestSubstring = function(s) {\n    let ans = 0;\n    let count = new Array(256).fill(0);\n    for (let i = 0, j = 0; i < s.length; i++) {\n        count[s.charCodeAt(i)]++;\n        while (count[s.charCodeAt(i)] > 1) {\n            count[s.charCodeAt(j++)]--;\n        }\n        ans = Math.max(ans, i - j + 1);\n    }\n    return ans;\n};\n```\n\n\n**Complexity**:\n* Time Complexity : `O(n)`\n* Space Complexity : `O(1)`\n\n**Feel free to leave a comment if something is confusing, or if you have any suggestions on how I can improve the post.**
['C', 'PHP', 'Python', 'Java', 'Go', 'JavaScript']