<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Blogger Hommie</title>
    <description>The latest articles on DEV Community by Blogger Hommie (@blogger_hommie_7f56db264e).</description>
    <link>https://dev.to/blogger_hommie_7f56db264e</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3961475%2Fc53bdc28-982e-43fc-b376-8e28b7657404.png</url>
      <title>DEV Community: Blogger Hommie</title>
      <link>https://dev.to/blogger_hommie_7f56db264e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blogger_hommie_7f56db264e"/>
    <language>en</language>
    <item>
      <title>LeetCode Solution: 3. Longest Substring Without Repeating Characters</title>
      <dc:creator>Blogger Hommie</dc:creator>
      <pubDate>Sun, 31 May 2026 17:24:33 +0000</pubDate>
      <link>https://dev.to/blogger_hommie_7f56db264e/leetcode-solution-3-longest-substring-without-repeating-characters-ldm</link>
      <guid>https://dev.to/blogger_hommie_7f56db264e/leetcode-solution-3-longest-substring-without-repeating-characters-ldm</guid>
      <description>&lt;h1&gt;
  
  
  3. Longest Substring Without Repeating Characters 🟡 Medium
&lt;/h1&gt;

&lt;p&gt;Hey Dev.to fam! 👋 Today, we're diving into a LeetCode classic that’s a fantastic introduction to a super common and powerful algorithm pattern: the &lt;strong&gt;Sliding Window&lt;/strong&gt;. This problem, "Longest Substring Without Repeating Characters," is a must-know for anyone looking to sharpen their algorithmic skills. It might seem tricky at first, but once you grasp the core idea, it opens doors to solving many similar problems!&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Problem Explanation
&lt;/h3&gt;

&lt;p&gt;Imagine you have a string of characters, like &lt;code&gt;"abcabcbb"&lt;/code&gt;. Your goal is to find the longest possible piece of that string (a &lt;strong&gt;substring&lt;/strong&gt;) where &lt;em&gt;every character in that piece is unique&lt;/em&gt;. No repeats allowed!&lt;/p&gt;

&lt;p&gt;Let's break down the examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1: &lt;code&gt;s = "abcabcbb"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  "abc" is a substring with no repeating characters. Its length is 3.&lt;/li&gt;
&lt;li&gt;  "bca" and "cab" are also valid substrings of length 3.&lt;/li&gt;
&lt;li&gt;  Can we do better? "abca" has 'a' repeated. "abcab" has 'a' and 'b' repeated.&lt;/li&gt;
&lt;li&gt;  So, the longest is 3.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Example 2: &lt;code&gt;s = "bbbbb"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Any substring longer than "b" will have repeating 'b's.&lt;/li&gt;
&lt;li&gt;  The longest substring without repeats is "b", with a length of 1.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Example 3: &lt;code&gt;s = "pwwkew"&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  "pw" -&amp;gt; length 2&lt;/li&gt;
&lt;li&gt;  "pww" -&amp;gt; 'w' repeats. Not valid.&lt;/li&gt;
&lt;li&gt;  "wke" -&amp;gt; length 3.&lt;/li&gt;
&lt;li&gt;  "kew" -&amp;gt; length 3.&lt;/li&gt;
&lt;li&gt;  "pwke" is NOT a substring, it's a subsequence (characters not contiguous). We're strictly looking for &lt;strong&gt;substrings&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  The longest is 3 (e.g., "wke").&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints to keep in mind:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The string &lt;code&gt;s&lt;/code&gt; can be empty (&lt;code&gt;0&lt;/code&gt; length) up to &lt;code&gt;5 * 10^4&lt;/code&gt; characters long.&lt;/li&gt;
&lt;li&gt;  It can contain English letters, digits, symbols, and spaces. This means we're dealing with a diverse set of characters, but their total possible count (e.g., ASCII range) is manageable.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. Intuition
&lt;/h3&gt;

&lt;p&gt;How would you try to solve this manually? You'd probably start checking substrings:&lt;br&gt;
"a" (len 1)&lt;br&gt;
"ab" (len 2)&lt;br&gt;
"abc" (len 3)&lt;br&gt;
"abca" - oh no, 'a' repeats!&lt;br&gt;
So, "abc" was the longest for that segment. What now? Do you start fresh from "b"?&lt;br&gt;
"b" (len 1)&lt;br&gt;
"bc" (len 2)&lt;br&gt;
"bca" (len 3)&lt;br&gt;
"bcab" - 'b' repeats!&lt;/p&gt;

&lt;p&gt;This "try, fail, reset" approach hints at something: when we hit a repeat, we don't necessarily need to throw away &lt;em&gt;everything&lt;/em&gt; we've built. We just need to remove the problematic part.&lt;/p&gt;

&lt;p&gt;This "expanding a window and shrinking it when things go wrong" is the core idea behind the &lt;strong&gt;Sliding Window&lt;/strong&gt; technique. We'll maintain a "window" of characters and try to expand it as much as possible. If we encounter a duplicate, we'll shrink the window from the other end until the duplicate is gone.&lt;/p&gt;

&lt;p&gt;To efficiently check for duplicates within our current window, a &lt;code&gt;HashSet&lt;/code&gt; (or &lt;code&gt;unordered_set&lt;/code&gt; in C++) is perfect because it allows for lightning-fast (average O(1) time) checks and removals.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Approach
&lt;/h3&gt;

&lt;p&gt;We'll use two pointers, &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt;, to define our "sliding window" &lt;code&gt;s[left...right]&lt;/code&gt;.&lt;br&gt;
We'll also use an &lt;code&gt;unordered_set&amp;lt;char&amp;gt;&lt;/code&gt; (let's call it &lt;code&gt;charSet&lt;/code&gt;) to keep track of unique characters currently within our window.&lt;/p&gt;

&lt;p&gt;Here's the step-by-step approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initialize:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;left = 0&lt;/code&gt;: This pointer marks the beginning of our current substring.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;maxLength = 0&lt;/code&gt;: This will store the maximum length found so far.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;charSet = {}&lt;/code&gt;: An empty set to store characters in the current window.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Iterate with &lt;code&gt;right&lt;/code&gt; pointer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Loop &lt;code&gt;right&lt;/code&gt; from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;s.length() - 1&lt;/code&gt;. The &lt;code&gt;right&lt;/code&gt; pointer expands our window to the right.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Handle Duplicates (Shrink the Window):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Inside the loop, before adding &lt;code&gt;s[right]&lt;/code&gt; to our &lt;code&gt;charSet&lt;/code&gt;, we check: &lt;code&gt;while (charSet.find(s[right]) != charSet.end())&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;  This &lt;code&gt;while&lt;/code&gt; loop runs &lt;strong&gt;only if &lt;code&gt;s[right]&lt;/code&gt; is ALREADY present in our &lt;code&gt;charSet&lt;/code&gt;&lt;/strong&gt;. This means &lt;code&gt;s[right]&lt;/code&gt; is a duplicate within our current window &lt;code&gt;s[left...right-1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  To remove the duplicate, we need to shrink the window from the &lt;code&gt;left&lt;/code&gt;. We do this by:

&lt;ul&gt;
&lt;li&gt;  Removing &lt;code&gt;s[left]&lt;/code&gt; from &lt;code&gt;charSet&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Incrementing &lt;code&gt;left&lt;/code&gt; (&lt;code&gt;left++&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;  This &lt;code&gt;while&lt;/code&gt; loop continues until &lt;code&gt;s[right]&lt;/code&gt; is no longer a duplicate in the &lt;em&gt;newly shrunk&lt;/em&gt; window.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add New Character (Expand the Window):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Once the &lt;code&gt;while&lt;/code&gt; loop finishes (meaning &lt;code&gt;s[right]&lt;/code&gt; is now unique in the window &lt;code&gt;s[left...right-1]&lt;/code&gt;), we can safely add &lt;code&gt;s[right]&lt;/code&gt; to &lt;code&gt;charSet&lt;/code&gt;: &lt;code&gt;charSet.insert(s[right])&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Update Maximum Length:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  After &lt;code&gt;s[right]&lt;/code&gt; is added, our current valid substring without repeating characters is &lt;code&gt;s[left...right]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Its length is &lt;code&gt;right - left + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Update &lt;code&gt;maxLength&lt;/code&gt;: &lt;code&gt;maxLength = max(maxLength, right - left + 1)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Return &lt;code&gt;maxLength&lt;/code&gt;:&lt;/strong&gt; After the &lt;code&gt;right&lt;/code&gt; pointer has traversed the entire string, &lt;code&gt;maxLength&lt;/code&gt; will hold the length of the longest substring without repeating characters.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's quickly trace &lt;code&gt;s = "abcabcbb"&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;right&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;s[right]&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;charSet&lt;/code&gt; (before add)&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;while&lt;/code&gt; loop action&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;charSet&lt;/code&gt; (after add)&lt;/th&gt;
&lt;th&gt;&lt;code&gt;left&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;curr_len&lt;/code&gt; (&lt;code&gt;r-l+1&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;&lt;code&gt;maxLength&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Current Window (for clarity)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;'a'&lt;/td&gt;
&lt;td&gt;{}&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;{'a'}&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;"a"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;'b'&lt;/td&gt;
&lt;td&gt;{'a'}&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;{'a', 'b'}&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;"ab"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;'c'&lt;/td&gt;
&lt;td&gt;{'a', 'b'}&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;{'a', 'b', 'c'}&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;"abc"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;'a'&lt;/td&gt;
&lt;td&gt;{'a', 'b', 'c'}&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;erase('a')&lt;/code&gt;, &lt;code&gt;l=1&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;{'b', 'c', 'a'}&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;"bca"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;'b'&lt;/td&gt;
&lt;td&gt;{'b', 'c', 'a'}&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;erase('b')&lt;/code&gt;, &lt;code&gt;l=2&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;{'c', 'a', 'b'}&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;"cab"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;'c'&lt;/td&gt;
&lt;td&gt;{'c', 'a', 'b'}&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;erase('c')&lt;/code&gt;, &lt;code&gt;l=3&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;{'a', 'b', 'c'}&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;"abc"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;'b'&lt;/td&gt;
&lt;td&gt;{'a', 'b', 'c'}&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;erase('a')&lt;/code&gt;, &lt;code&gt;l=4&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;erase('b')&lt;/code&gt;, &lt;code&gt;l=5&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;{'c', 'b'}&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;"cb"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;'b'&lt;/td&gt;
&lt;td&gt;{'c', 'b'}&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;erase('c')&lt;/code&gt;, &lt;code&gt;l=6&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;erase('b')&lt;/code&gt;, &lt;code&gt;l=7&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;{'b'}&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;"b"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Final &lt;code&gt;maxLength&lt;/code&gt; is 3. Perfect!&lt;/p&gt;




&lt;h3&gt;
  
  
  5. Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;lengthOfLongestSubstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// An unordered_set is perfect for quickly checking for duplicates&lt;/span&gt;
        &lt;span class="c1"&gt;// and storing unique characters in our current window.&lt;/span&gt;
        &lt;span class="n"&gt;unordered_set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;charSet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// The 'right' pointer expands our window&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// If the character at 'right' is already in our set,&lt;/span&gt;
            &lt;span class="c1"&gt;// it means we have a duplicate within our current window.&lt;/span&gt;
            &lt;span class="c1"&gt;// We need to shrink the window from the 'left' until the duplicate is removed.&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;charSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;charSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;charSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;erase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Remove the character at 'left'&lt;/span&gt;
                &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// Move 'left' pointer forward&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// Once the character at 'right' is unique in the window,&lt;/span&gt;
            &lt;span class="c1"&gt;// add it to our set.&lt;/span&gt;
            &lt;span class="n"&gt;charSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

            &lt;span class="c1"&gt;// Update the maximum length found so far.&lt;/span&gt;
            &lt;span class="c1"&gt;// Current window length is (right - left + 1).&lt;/span&gt;
            &lt;span class="n"&gt;maxLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxLength&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  6. Time &amp;amp; Space Complexity Analysis
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(N)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  The &lt;code&gt;right&lt;/code&gt; pointer iterates through the string &lt;code&gt;s&lt;/code&gt; exactly once, performing &lt;code&gt;N&lt;/code&gt; increments.&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;left&lt;/code&gt; pointer also moves forward at most &lt;code&gt;N&lt;/code&gt; times in total over the entire execution. Each character is added to the &lt;code&gt;charSet&lt;/code&gt; once (by &lt;code&gt;right&lt;/code&gt;) and removed from the &lt;code&gt;charSet&lt;/code&gt; at most once (by &lt;code&gt;left&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;unordered_set&lt;/code&gt; operations (&lt;code&gt;insert&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;erase&lt;/code&gt;) take &lt;strong&gt;average O(1)&lt;/strong&gt; time. In the worst case (due to hash collisions), they can degrade to O(N), but for character sets (like ASCII), collisions are rare enough that the average case holds true.&lt;/li&gt;
&lt;li&gt;  Since both pointers traverse the string at most once, and set operations are constant time on average, the total time complexity is &lt;strong&gt;O(N)&lt;/strong&gt;, where &lt;code&gt;N&lt;/code&gt; is the length of the string &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(min(N, M)) or O(1)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  The &lt;code&gt;charSet&lt;/code&gt; stores unique characters present in the current sliding window.&lt;/li&gt;
&lt;li&gt;  In the worst case, all characters in &lt;code&gt;s&lt;/code&gt; might be unique, so the set could potentially store up to &lt;code&gt;N&lt;/code&gt; characters.&lt;/li&gt;
&lt;li&gt;  However, the problem constraints state that &lt;code&gt;s&lt;/code&gt; consists of English letters, digits, symbols, and spaces. This means the total number of &lt;em&gt;possible&lt;/em&gt; unique characters (let's call it &lt;code&gt;M&lt;/code&gt;) is limited (e.g., ASCII has 128 or 256 characters).&lt;/li&gt;
&lt;li&gt;  Therefore, the maximum size of &lt;code&gt;charSet&lt;/code&gt; is bounded by &lt;code&gt;min(N, M)&lt;/code&gt;. Since &lt;code&gt;M&lt;/code&gt; (e.g., 128 for ASCII) is a constant, we often simplify the space complexity to &lt;strong&gt;O(1)&lt;/strong&gt; in competitive programming contexts, implying constant extra space relative to the input size &lt;code&gt;N&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. Key Takeaways
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Sliding Window Pattern:&lt;/strong&gt; This problem is a quintessential example of the "Sliding Window" technique. It's incredibly useful for problems involving subarrays or substrings where you need to find something (max, min, count) within a contiguous range.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Two Pointers (&lt;code&gt;left&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt;):&lt;/strong&gt; The &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt; pointers define the current window. &lt;code&gt;right&lt;/code&gt; expands the window, and &lt;code&gt;left&lt;/code&gt; contracts it when a condition (like uniqueness) is violated.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;&lt;code&gt;unordered_set&lt;/code&gt; for Uniqueness Checks:&lt;/strong&gt; An &lt;code&gt;unordered_set&lt;/code&gt; (or &lt;code&gt;HashSet&lt;/code&gt; in other languages) is your best friend for quickly checking if an element exists and maintaining a collection of unique items. Its average O(1) time complexity for &lt;code&gt;insert&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;, and &lt;code&gt;erase&lt;/code&gt; is crucial for optimizing the sliding window.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Understanding &lt;code&gt;while&lt;/code&gt; vs. &lt;code&gt;if&lt;/code&gt;:&lt;/strong&gt; The &lt;code&gt;while&lt;/code&gt; loop for shrinking the window (&lt;code&gt;while (charSet.find(s[right]) != charSet.end())&lt;/code&gt;) is critical. It ensures that &lt;em&gt;all&lt;/em&gt; instances of the duplicate character (and any characters preceding it) are removed from the &lt;code&gt;left&lt;/code&gt; until &lt;code&gt;s[right]&lt;/code&gt; is truly unique in the current window. An &lt;code&gt;if&lt;/code&gt; here would only remove one character and might leave other duplicates, leading to incorrect results.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  8. Submission Details
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Author Account:&lt;/strong&gt; ahB1IK4YXF&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Publishing Time:&lt;/strong&gt; 2026-05-31 22:54:05&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
