<?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: blakeahalt</title>
    <description>The latest articles on DEV Community by blakeahalt (@blakeahalt).</description>
    <link>https://dev.to/blakeahalt</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%2F1028661%2F5ba3f69b-fb3b-4d1f-8c36-155a2e5341b9.png</url>
      <title>DEV Community: blakeahalt</title>
      <link>https://dev.to/blakeahalt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blakeahalt"/>
    <language>en</language>
    <item>
      <title>Leetcode 435. Non-overlapping Intervals</title>
      <dc:creator>blakeahalt</dc:creator>
      <pubDate>Mon, 17 Apr 2023 09:52:19 +0000</pubDate>
      <link>https://dev.to/blakeahalt/leetcode-435-non-overlapping-intervals-5437</link>
      <guid>https://dev.to/blakeahalt/leetcode-435-non-overlapping-intervals-5437</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vh6gufbutu7jiyeglez.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vh6gufbutu7jiyeglez.jpg" alt="Description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This solution works by first sorting the &lt;code&gt;intervals&lt;/code&gt; array in ascending order based on the starti of each interval.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;intervals.sort((a,b)=&amp;gt; a[0]-b[0])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sorted array becomes:&lt;br&gt;
&lt;code&gt;intervals = [[1,2],[1,3],[2,3],[3,4]]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple way to visualize the intervals array:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo9it16ktbpiue88pah4g.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo9it16ktbpiue88pah4g.jpg" alt="intervals array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we initialize a variable called &lt;code&gt;prevEnd&lt;/code&gt; to the endi of the first interval, or intervals[0][1], and a variable called &lt;code&gt;count&lt;/code&gt; to 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let prevEnd = intervals[0][1]
let count = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what that looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy26993c5qrq7eb0jgk7a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy26993c5qrq7eb0jgk7a.jpg" alt="first code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For loops typically begin at i=0, but notice our for loop begins at i=1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; for (let i=1; i&amp;lt;intervals.length; i++) {
...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because we've already used the first element of the intervals array, which represents i=0, to set the variable &lt;code&gt;prevEnd = intervals[0][1]&lt;/code&gt;. In our case, prevEnd is initialized at 2.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwj3iumnij94lgg84pkh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwj3iumnij94lgg84pkh.jpg" alt="image3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So starting at i=1, every iteration of the for loop compares the values between &lt;code&gt;intervals[i][0]&lt;/code&gt; and &lt;code&gt;prevEnd&lt;/code&gt; to determine which code in the if/else statement to execute. Note that the value of prevEnd will get updated each iteration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgh0ejdrkzh87qow9q2pv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgh0ejdrkzh87qow9q2pv.jpg" alt="if/else"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;intervals[i][0]&lt;/code&gt; is greater than or equal to &lt;code&gt;prevEnd&lt;/code&gt;, it means that the interval does not overlap with the previous interval and prevEnd is updated to the endi of the current interval, or &lt;code&gt;intervals[i][1]&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (intervals[i][0] &amp;gt;= prevEnd) {
  prevEnd = intervals[i][1]
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Else, if &lt;code&gt;intervals[i][0]&lt;/code&gt; is less than &lt;code&gt;prevEnd&lt;/code&gt;, it means that the interval overlaps with the previous interval. So, the variable &lt;code&gt;count&lt;/code&gt; is incremented by one and &lt;code&gt;prevEnd&lt;/code&gt; is updated to the minimum value between &lt;code&gt;intervals[i][1]&lt;/code&gt; and &lt;code&gt;prevEnd&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
else
  count++
  prevEnd = Math.min(intervals[i][1], prevEnd)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's walk through the for loop and see how the if/else statement gets applied at each iteration.&lt;/p&gt;

&lt;p&gt;In the first iteration, when i=1, we can see that intervals[i][0] &amp;lt;= prevEnd.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz8zyvdz9xigvhezgg9w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz8zyvdz9xigvhezgg9w.jpg" alt="i=1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;According to the if/else statement, we increment the count by 1 and set prevEnd to the smaller value between &lt;code&gt;intervals[i][1]&lt;/code&gt; and &lt;code&gt;prevEnd&lt;/code&gt;. In this case intervals[1][1]=3 and prevEnd=2, so prevEnd remains the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3c9vtxt41uomd7y5i92.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3c9vtxt41uomd7y5i92.jpg" alt="if statement"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the next iteration when i=2, prevEnd = intervals[i][0].&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8am4jh4syh5giqf8hnh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8am4jh4syh5giqf8hnh.png" alt="i=2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;According to the if/else statement, we set prevEnd to intervals[i][1]. In this case intervals[2][1]=3, so prevEnd updates its value to 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fza5dgd9zi30m2a6usoiy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fza5dgd9zi30m2a6usoiy.png" alt="else statement"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the last iteration when i=3, prevEnd = intervals[i][0] once again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn37gd96jkvbpcni450qz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn37gd96jkvbpcni450qz.png" alt="i=3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since, there are no more iterations, we exit the for loop and return our count, which is 1. &lt;/p&gt;

&lt;p&gt;A visual review of our process shows that removing the second element of the array makes the rest of the array non-overlapping. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1biwwb7doe4nd80qkpk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1biwwb7doe4nd80qkpk.jpg" alt="visual review"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The time complexity is O(nlog n), where n is the number of intervals in the input array. This is because the code first sorts the input array, which takes O(nlog n) time, and then iterates through the array once, performing constant-time operations on each element.&lt;/p&gt;

&lt;p&gt;The space complexity is O(1), which means that the amount of memory used by the code is constant, regardless of the size of the input. This is because the code only uses a few constant-size variables to store information, and does not create any new arrays or objects.&lt;/p&gt;

&lt;p&gt;Hope this was helpful.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>leetcode</category>
      <category>solution</category>
      <category>interval</category>
    </item>
    <item>
      <title>Leetcode 409: Longest Palindrome</title>
      <dc:creator>blakeahalt</dc:creator>
      <pubDate>Tue, 04 Apr 2023 08:14:42 +0000</pubDate>
      <link>https://dev.to/blakeahalt/leetcode-409-longest-palindrome-2o3l</link>
      <guid>https://dev.to/blakeahalt/leetcode-409-longest-palindrome-2o3l</guid>
      <description>&lt;p&gt;&lt;a href="https://leetcode.com/problems/longest-palindrome/" rel="noopener noreferrer"&gt;Problem 409: Longest Palindrome&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa6itookqozcm6c49s6d.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxa6itookqozcm6c49s6d.jpeg" alt="Solution to Problem 409: Longest Palindrome"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given a string 's' which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive.&lt;/p&gt;

&lt;p&gt;This solution iterates through the characters of the input string s, and for each character, checks if it is already in the Set 'set'. If the character is already in the Set, it means that it can be part of a palindrome, so the count is incremented by 2 (because a palindrome has two identical halves). The character is also deleted from the Set, so that it cannot be counted again. If the character is not in the Set, it is added to the Set.&lt;/p&gt;

&lt;p&gt;At the end of the loop, if the length of the input string s is equal to the count, it means that all characters can be used to form a palindrome, so the count is returned as is. If the length of s is not equal to the count, it means that there are some characters left in the Set that can be used to form a palindrome with the existing characters, so the count is incremented by 1 and returned.&lt;/p&gt;

&lt;p&gt;We begin by initializing 'count' to 0 and 'set' to a new empty Set.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

let count = 0
let set = new Set()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, we start iterating through a for loop.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

for (let char of s) {
  ...
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Each iteration checks whether each character is already in the set or not. &lt;br&gt;
If the character is NOT in the set we add it to the set.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

for (let char of s) {
  if {
    ...
  } else 
    set.add(char)
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the character IS in the set, the count is incremented by 2 and deleted from the set.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

for (let char of s) {
  if (set.has(char) {
    count += 2
    set.delete(char)
  } else 
    ...
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;At the end of the loop we need to return the correct count, indicating the longest possible palindrome. Knowing that a palindrome can contain at most 1 single character, we can use a ternary operator to account for this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

return count &amp;lt; s.length ? count + 1 : count


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the count is less than s.length we know there's at least one single character. In our case, count = 6 and s.length = 8. The longest possible palindrome we could return would be a combination  of (dcc[x]ccd), where [x] represents a single character from our set. It doesn't matter if it were (dccaccd) or (dccbccd) since the count would still be equal to 7. The [x] represents the '+ 1' in our ternary 'count + 1'.&lt;/p&gt;

&lt;p&gt;If the count is NOT less than s.length that means our string has an even count and every character has a matching character creating a complete palindrome. Since it's impossible for the count to be greater than s.length, when the count is not less than s.length it can only mean that count = s.length. Our ternary could be written as either:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

return count &amp;lt; s.length ? count + 1: count


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

return count &amp;lt; s.length ? count + 1: s.length


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our final code looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

var longestPalindrome = function(s) {
    let count = 0
    let set = new Set()

    for (let char of s) {
        if (set.has(char)) {
            count+=2
            set.delete(char)
        } else {
            set.add(char)
        }
    }
return count !== s.length ? count + 1 : count
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The time complexity of this code is O(n), where n is the length of the input string s. This is because the code iterates through each character in the string only once and performs constant time operations on each character.&lt;/p&gt;

&lt;p&gt;The space complexity of this code is also O(n), where n is the length of the input string s. This is because the code creates a set to store characters that appear an odd number of times in the string, and the size of the set can be at most half the length of the input string (in the case where all characters in the string appear exactly twice). Therefore, the space required to store the set is proportional to the length of the input string. Additionally, the code uses a constant amount of additional space to store the count variable.&lt;/p&gt;

&lt;p&gt;Hope this was helpful. Please leave a comment if anything looks incorrect or if you have any questions.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>javascript</category>
      <category>solution</category>
      <category>palindrome</category>
    </item>
    <item>
      <title>LeetCode 3: Longest Substring Without Repeating Characters</title>
      <dc:creator>blakeahalt</dc:creator>
      <pubDate>Fri, 17 Feb 2023 11:21:27 +0000</pubDate>
      <link>https://dev.to/blakeahalt/3-longest-substring-without-repeating-characters-solution-4645</link>
      <guid>https://dev.to/blakeahalt/3-longest-substring-without-repeating-characters-solution-4645</guid>
      <description>&lt;p&gt;&lt;a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/" rel="noopener noreferrer"&gt;Problem 3. Longest Substring without Repeating Characters&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqb767ewx7l5i5vjsc07.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqb767ewx7l5i5vjsc07.jpg" alt="Visual solution for LeetCode problem 3. Longest Substring Without Repeating Characters" width="800" height="952"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Begin by defining the stack variable and max variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let stack = new Set()
let max = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using a for loop we can begin adding each element from the input string. At the same time, we want to keep track of the longest substring in the max variable by comparing the current max size and the size of the stack up until that point.&lt;/p&gt;

&lt;p&gt;Our full code, so far, would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var lengthOfLongestSubstring = function(s) {
  let stack = new Set()
  let max = 0

  for (let i=0; i&amp;lt;s.length; i++) {
    stack.add(s[i])
    max = Math.max(max, stack.size)
  }
return max
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a good starting point, but now we need to account for any repeating characters.&lt;/p&gt;

&lt;p&gt;Once we get to the third index, you can see that the stack would be:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;stack = [a,b,c,c] &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is not what we want because the problem is asking for the longest substring &lt;strong&gt;without&lt;/strong&gt; repeating characters.&lt;/p&gt;

&lt;p&gt;To resolve this we need to delete all of the elements in our current stack until there are no repeated characters and then continue the logic we coded above (adding and keeping track of the max).&lt;/p&gt;

&lt;p&gt;In order to do this we need to add another variable called left. Our variables can be updated to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  let stack = new Set()
  let max = 0
  let left = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This left variable is going to point to an element in the string that we delete in our stack. Currently: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;left = 0, so s[left] = 'a'.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using a while loop, we can add logic to delete all of the elements preceding the repeated character. In our case, since our stack = [a,b,c,c] we want to delete stack = [a,b,c] and be left with stack = [c].&lt;/p&gt;

&lt;p&gt;The while loop looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  while (stack.has(s[i])) {
    stack.delete(s[left])
    left++
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will pause at the current i-th iteration in our for loop and execute until true. &lt;/p&gt;

&lt;p&gt;Looking at the purple section in the image we can see we're paused at i=3 because our current stack now has a repeated character 'c'. Whenever a repeated character occurs our code will enter the while loop and execute the commands (delete and left++) until false.  &lt;/p&gt;

&lt;p&gt;It's important to understand that the while loop will keep performing the (delete and left++) commands until the stack no longer has a condition where stack.has(s[i]).&lt;/p&gt;

&lt;p&gt;We'll come back to this. Let's run it back to the beginning.&lt;/p&gt;

&lt;p&gt;Here is our full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var lengthOfLongestSubstring = function(s) {
  let stack = new Set()
  let max = 0

  for (let i=0; i&amp;lt;s.length; i++) {
    while (stack.has(s[i])) {
      stack.delete(s[left])
      left++
    }
    stack.add(s[i])
    max = Math.max(max, stack.size)
  }
return max
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take note of where we've placed our while loop. The reason why we we're able to iterate from i=0 to i=3 without entering the while loop is because stack.has(s[i]) was never true.&lt;/p&gt;

&lt;p&gt;At i=0, does stack have s[0] or ('a')? No, so we add 'a' to the stack and our for loop goes to i=1. Now our stack=[a].&lt;br&gt;
At i=1, does our stack have s[1] or ('b')? No, so we add 'b' to the stack and our for loop goes to i=2. Now our stack=[a,b].&lt;br&gt;
At i=2, does our stack have s[2] or ('c')? No, so we add 'c' to the stack and our for loop goes to i=3. Now our stack=[a,b,c].&lt;br&gt;
At i=3, does our stack have s[3] or ('c')? &lt;strong&gt;YES&lt;/strong&gt;, and this will trigger our while loop.&lt;/p&gt;

&lt;p&gt;Once we have triggered the while loop, as we mentioned above it will start to delete elements from the stack and add 1 to the left variable.&lt;/p&gt;

&lt;p&gt;Let's take a closer look at i=3. &lt;/p&gt;

&lt;p&gt;Well, after i=2, our stack=[a,b,c]. Now, looking at when i=3, our while loop has checked that, yes, the stack has s[3] or 'c' and begins to delete and add one to left.&lt;/p&gt;

&lt;p&gt;what does that look like?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stack.delete(s[left])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will delete s[left], which currently left=0. &lt;br&gt;
  So, we delete s[0] or 'a'. &lt;br&gt;
Then we add one to left. &lt;br&gt;
  left=1.&lt;/p&gt;

&lt;p&gt;Ok, looking at the code we continue down to stack.add(s[i]) and update the max variable, right? &lt;strong&gt;NO!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because we're in a while loop, we don't exit until the condition stack.has(s[i]) is false. After updating the previous sequence we see that stack=[b,c], left=1 and s[i]= 'c' since we're still paused at i=3.&lt;/p&gt;

&lt;p&gt;The while loop continues checking if our stack has 'c'. It does, so we delete s[left], which is now s[1] from the stack and iterate left by 1. &lt;br&gt;
  Now our stack=[c] and left=2.&lt;/p&gt;

&lt;p&gt;Again, the while loop checks if our stack has 'c'. It does, so we delete s[left], which is now s[2] from the stack and iterate left by 1. &lt;br&gt;
  Now our stack=[] and left=3.&lt;/p&gt;

&lt;p&gt;Again, the while loop checks if our stack has 'c'. It doesn't because our current stack is empty. In the code, we exit the while loop and execute the next part of the code which is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  stack.add(s[i])
  max = Math.max(max, stack.size)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we add s[i] or 'c' to our stack and calculate the max. Math.max takes the bigger value between max, currently 3 from stack=[a,b,c], and the current stack.size, which is 1. &lt;/p&gt;

&lt;p&gt;Now, stack=[c] and max=3&lt;/p&gt;

&lt;p&gt;This concludes i=3 and the for loop continues to i=4.&lt;/p&gt;

&lt;p&gt;At i=0, does stack have s[4] or ('a')? No, so we add 'a' to the stack and our for loop goes to i=5. Now our stack=[c,a] and we can see that max = Math.max(3,2), 2 being the stack.size of stack=[c,a]. So, still max=3.&lt;/p&gt;

&lt;p&gt;Now, stack=[c,a] and max=3&lt;/p&gt;

&lt;p&gt;At this point you should be able to follow the remainder of the for loop. We add another element at i=5 and enter the while loop at i=6.&lt;/p&gt;

&lt;p&gt;When our for loop ends we check the max value and see that it's 3, which indicates that the longest substring our code found was length 3. In this case, the strings equal to 3 were [a,b,c] and [c,a,b] but the question was not asking for which strings, only the longest length.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;time complexity&lt;/strong&gt; of this function is &lt;strong&gt;O(n)&lt;/strong&gt;, where n is the length of the input string s.&lt;/p&gt;

&lt;p&gt;The reason for this is that the function iterates through each character in the string once using a for loop. Within the loop, it performs a constant number of operations, which include set operations (adding, deleting, and checking for membership in the set) and arithmetic operations (comparing and taking the maximum).&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;space complexity&lt;/strong&gt; of this function is &lt;strong&gt;O(min(n,m))&lt;/strong&gt;, where n is the length of the input string s and m is the size of the character set (i.e., the number of distinct characters in s).&lt;/p&gt;

&lt;p&gt;The reason for this is that the function uses a set stack to store the characters in the current substring it is considering. The size of the set can be at most the size of the character set, which is O(m). In the worst case, where all characters in the input string are distinct, the size of the set would be equal to n.&lt;/p&gt;

&lt;p&gt;In addition to the set, the function also uses a constant amount of additional space O(1) to store variables such as left and max. However, this does not affect the space complexity since it's a constant.&lt;/p&gt;

&lt;p&gt;Therefore, the space complexity of the function is O(min(n,m)), since the overall space used by the function is dominated by the size of the set, which is the minimum of n and m.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;best case&lt;/em&gt; scenario for this function is when the input string s contains only one character or is empty. In this case, the for loop will execute only once, and the while loop will not execute at all, resulting in a time complexity of O(1) and a space complexity of O(1).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lengthOfLongestSubstring('a') // returns 1
lengthOfLongestSubstring('') // returns 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;worst case&lt;/em&gt; scenario of the function is time complexity O(n) and space complexity O(m), where n is the length of the input string s and m is the stack size of all unique characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lengthOfLongestSubstring("abcdefghijklmnopqrstuvwxyz") // In this case, the input string contains all unique characters, and the `stack` set will have to accommodate all characters of the input string. Therefore, the worst case space complexity is O(n), not O(m).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this was helpful. Please leave a comment if anything looks incorrect or if you have any questions.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
