{"id":910,"date":"2025-08-20T12:28:16","date_gmt":"2025-08-20T06:58:16","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=910"},"modified":"2025-08-20T12:28:17","modified_gmt":"2025-08-20T06:58:17","slug":"longest-substring-without-repeating-characters","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/","title":{"rendered":"Longest Substring Without Repeating Characters | Leetcode 3 | Optimal Sliding Window Solution"},"content":{"rendered":"\n<p>The problem\u00a0<strong>\u201cLongest Substring Without Repeating Characters\u201d<\/strong>\u00a0asks for the length of the\u00a0<strong>longest substring<\/strong>\u00a0in a string\u00a0<code>s<\/code>\u00a0that contains\u00a0<strong>no duplicate characters<\/strong>. A straightforward\u00a0<strong>brute force<\/strong>\u00a0approach checks all substrings, while the\u00a0<strong>optimal<\/strong>\u00a0solution uses a\u00a0<strong>sliding window<\/strong>\u00a0with a\u00a0<strong>hash map<\/strong>\u00a0to achieve linear time.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>, find the length of the&nbsp;<strong>longest<\/strong>&nbsp;<strong>substring<\/strong>&nbsp;without duplicate characters.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/longest-substring-without-repeating-characters\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> s = \"abcabcbb\"<br><strong>Output:<\/strong> 3<br><strong>Explanation:<\/strong> The answer is \"abc\", with the length of 3.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> s = \"bbbbb\"<br><strong>Output:<\/strong> 1<br><strong>Explanation:<\/strong> The answer is \"b\", with the length of 1.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> s = \"pwwkew\"<br><strong>Output:<\/strong> 3<br><strong>Explanation:<\/strong> The answer is \"wke\", with the length of 3.<br>Notice that the answer must be a substring, \"pwke\" is a subsequence and not a substring.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>0 &lt;= s.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>s<\/code>\u00a0consists of English letters, digits, symbols and spaces.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-603feea8-13ec-43a1-8320-541a1549a27e\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"false\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#0-brute-force-check-all-substrings-\" style=\"\">Brute Force (Check All Substrings)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#1-intuition-and-approach-\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#2-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#3-code-explanation-\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#4-time-and-space-complexity-\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#5-conclusion-\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#6-optimal-sliding-window-hash-map-\" style=\"\">Optimal (Sliding Window + Hash Map)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#7-intuition-\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#8-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#9-code-explanation-\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#10-time-and-space-complexity-\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#11-conclusion-\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/longest-substring-without-repeating-characters\/#12-common-pitfalls-and-tips-\" style=\"\">Common\u00a0Pitfalls\u00a0and\u00a0Tips<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-check-all-substrings-\">Brute Force (<strong>Check All Substrings<\/strong>)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-and-approach-\"><strong>Intuition and Approach<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For every start index\u00a0<code>i<\/code>, expand the end index\u00a0<code>j<\/code>\u00a0until a\u00a0<strong>repeat<\/strong>\u00a0is found.<\/li>\n\n\n\n<li>Maintain a\u00a0<strong>set\/dictionary<\/strong>\u00a0to track seen characters within the current substring.<\/li>\n\n\n\n<li>Update the\u00a0<strong>maximum length<\/strong>\u00a0whenever a longer valid substring is found.<\/li>\n\n\n\n<li>This is simple but runs in\u00a0<strong>O(n^2)<\/strong>\u00a0time.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def lengthOfLongestSubstring(self, s: str) -&gt; int:\n        if len(s) == 0:\n            return 0\n        maxans = 0\n        for i in range(len(s)):\n            set = {}\n            for j in range(i, len(s)):\n                if s[j] in set:\n                    break\n                maxans = max(maxans, j - i + 1)\n                set[s[j]] = 1\n        return maxans\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">lengthOfLongestSubstring<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">s<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(s) == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        maxans = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(s)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #4EC9B0\">set<\/span><span style=\"color: #D4D4D4\"> = {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> j <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(i, <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(s)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> s[j] <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">set<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                maxans = <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(maxans, j - i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                set[s[j]] = <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> maxans<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-explanation-\"><strong>Code Explanation<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Outer loop picks the start index; inner loop grows the substring until a\u00a0<strong>repeat<\/strong>.<\/li>\n\n\n\n<li>A dictionary acts like a\u00a0<strong>set<\/strong>\u00a0to check presence in O(1).<\/li>\n\n\n\n<li>Updates\u00a0<code>maxans<\/code>\u00a0whenever the current window extends without duplicates.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-time-and-space-complexity-\"><strong>Time and Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong>\u00a0O(n^2)<\/li>\n\n\n\n<li><strong>Space:<\/strong>\u00a0O(min(n, \u03a3)) for the set (\u03a3 = character set size)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-conclusion-\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<p>Great for&nbsp;<strong>intuition<\/strong>, but not suitable for&nbsp;<strong>large strings<\/strong>&nbsp;due to quadratic time.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-optimal-sliding-window-hash-map-\">Optimal (<strong>Sliding Window + Hash Map<\/strong>)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-intuition-\"><strong>Intuition<\/strong><\/h3>\n\n\n\n<p>Use a&nbsp;<strong>sliding window<\/strong>&nbsp;with two pointers (<code>left<\/code>,&nbsp;<code>right<\/code>) and a&nbsp;<strong>hash map<\/strong>&nbsp;that stores the&nbsp;<strong>last seen index<\/strong>&nbsp;of each character:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As\u00a0<code>right<\/code>\u00a0moves, if\u00a0<code>s[right]<\/code>\u00a0is already in the window,\u00a0<strong>move\u00a0<code>left<\/code><\/strong>\u00a0to\u00a0<code>max(left, last_index + 1)<\/code>\u00a0to skip past the duplicate.<\/li>\n\n\n\n<li>Update the\u00a0<strong>answer<\/strong>\u00a0with the current window size\u00a0<code>right - left + 1<\/code>.<\/li>\n\n\n\n<li>This ensures each character is processed at most\u00a0<strong>twice<\/strong>, yielding\u00a0<strong>O(n)<\/strong>\u00a0time.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def lengthOfLongestSubstring(self, s: str) -&gt; int:\n        hash_map = dict()\n        left = 0\n        right = 0\n        length = 0\n        n = len(s)\n        while right &lt; n:\n            if s[right] in hash_map:\n                left = max(hash_map[s[right]] + 1, left)\n            hash_map[s[right]] = right\n            length = max(length, right - left + 1)\n            right += 1\n        return length\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">lengthOfLongestSubstring<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">s<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        hash_map = <\/span><span style=\"color: #4EC9B0\">dict<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        left = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        right = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        length = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(s)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> right &lt; n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> s[right] <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> hash_map:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                left = <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(hash_map[s[right]] + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, left)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            hash_map[s[right]] = right<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            length = <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(length, right - left + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            right += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> length<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-explanation-\"><strong>Code Explanation<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>hash_map[ch] = last seen index of ch<\/code>.<\/li>\n\n\n\n<li>When encountering a\u00a0<strong>duplicate<\/strong>\u00a0inside the current window,\u00a0<strong>jump\u00a0<code>left<\/code><\/strong>\u00a0forward to avoid shrinking one-by-one.<\/li>\n\n\n\n<li><code>length<\/code>\u00a0tracks the\u00a0<strong>best window size<\/strong>\u00a0seen so far.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-time-and-space-complexity-\"><strong>Time and Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong>\u00a0O(n) &#8211; each index is visited at most twice (once by\u00a0<code>right<\/code>, possibly once by\u00a0<code>left<\/code>)<\/li>\n\n\n\n<li><strong>Space:<\/strong>\u00a0O(min(n, \u03a3)) for the hash map<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-conclusion-\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<strong>sliding window<\/strong>&nbsp;with a&nbsp;<strong>hash map<\/strong>&nbsp;is the&nbsp;<strong>optimal<\/strong>&nbsp;solution:&nbsp;<strong>clean<\/strong>,&nbsp;<strong>efficient<\/strong>, and perfect for production or interviews.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"12-common-pitfalls-and-tips-\">Common&nbsp;<strong>Pitfalls<\/strong>&nbsp;and&nbsp;<strong>Tips<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always ensure\u00a0<code>left<\/code>\u00a0only moves\u00a0<strong>forward<\/strong>:\u00a0<code>left = max(left, last_index + 1)<\/code>.<\/li>\n\n\n\n<li>The hash map should store the\u00a0<strong>latest index<\/strong>\u00a0of each character.<\/li>\n\n\n\n<li>Works seamlessly for all ASCII\/Unicode as long as memory allows.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Use&nbsp;<strong>Brute Force<\/strong>&nbsp;to understand the mechanics; use the&nbsp;<strong>Optimal Sliding Window<\/strong>&nbsp;for performance.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem\u00a0\u201cLongest Substring Without Repeating Characters\u201d\u00a0asks for the length of the\u00a0longest substring\u00a0in a string\u00a0s\u00a0that contains\u00a0no duplicate characters. A straightforward\u00a0brute force\u00a0approach checks all substrings, while the\u00a0optimal\u00a0solution uses a\u00a0sliding window\u00a0with a\u00a0hash map\u00a0to achieve linear time. Given a string&nbsp;s, find the length of the&nbsp;longest&nbsp;substring&nbsp;without duplicate characters. Here&#8217;s the [Problem Link] to begin with. Example 1: Input: s =<\/p>\n","protected":false},"author":1,"featured_media":912,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[19,40],"class_list":{"0":"post-910","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-medium","10":"tag-sliding-window-and-two-pointers"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/08\/longest-substring-without-repeating-characters-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=910"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/910\/revisions"}],"predecessor-version":[{"id":913,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/910\/revisions\/913"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/912"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}