<?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>Forem: Ashwat RK</title>
    <description>The latest articles on Forem by Ashwat RK (@ashwat_rk_9d3b3dfa5c2a03b).</description>
    <link>https://forem.com/ashwat_rk_9d3b3dfa5c2a03b</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%2F3356914%2Fbecfe148-dc96-4e24-99d4-474f196fbd3f.jpeg</url>
      <title>Forem: Ashwat RK</title>
      <link>https://forem.com/ashwat_rk_9d3b3dfa5c2a03b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ashwat_rk_9d3b3dfa5c2a03b"/>
    <language>en</language>
    <item>
      <title>Day 1 of Solving LeetCode Problems: Tackling Problem #3</title>
      <dc:creator>Ashwat RK</dc:creator>
      <pubDate>Tue, 14 Oct 2025 17:22:26 +0000</pubDate>
      <link>https://forem.com/ashwat_rk_9d3b3dfa5c2a03b/day-1-of-solving-leetcode-problems-tackling-problem-3-4gb4</link>
      <guid>https://forem.com/ashwat_rk_9d3b3dfa5c2a03b/day-1-of-solving-leetcode-problems-tackling-problem-3-4gb4</guid>
      <description>&lt;p&gt;Today, I decided to challenge myself with LeetCode Problem 3: Longest Substring Without Repeating Characters. As someone who enjoys solving algorithmic problems, I thought this would be a walk in the park—a cup of coffee while coding, so to speak.&lt;br&gt;
Well… I was in for a little surprise.&lt;br&gt;
After writing my initial solution, I ran it against the LeetCode test cases. Out of 988 cases, 987 passed. But one stubborn test case failed. I realized it was probably a load test meant to check how efficient my solution really was. Even though it didn’t fully pass, this exercise taught me a lot about handling substrings, avoiding repeated characters, and thinking about efficiency.&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%2Fago38iixqijl1w4hs2rk.png" 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%2Fago38iixqijl1w4hs2rk.png" alt=" " width="800" height="431"&gt;&lt;/a&gt;&lt;br&gt;
Let me walk you through my journey.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Understanding the Problem&lt;/strong&gt;&lt;br&gt;
The problem sounds simple at first:&lt;br&gt;
Given a string, find the length of the longest substring without repeating characters.&lt;br&gt;
For example:&lt;br&gt;
• "abcabcbb" → Longest substring: "abc" → length = 3&lt;br&gt;
• "bbbbb" → Longest substring: "b" → length = 1&lt;br&gt;
• "pwwkew" → Longest substring: "wke" → length = 3&lt;br&gt;
Sounds easy, right? But implementing it efficiently is a bit tricky.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;My First Attempt&lt;/strong&gt;&lt;br&gt;
Here’s the first function I wrote:&lt;br&gt;
var lengthOfLongestSubstring = function (str) {&lt;br&gt;
    let arr = str.split('');&lt;br&gt;
    let mainLeft = 0;&lt;br&gt;
    let subLeft = 0;&lt;br&gt;
    let right = subLeft + 1;&lt;br&gt;
    let subStrArr = [];&lt;br&gt;
    let subStr = 0;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;subStrArr.push(arr[subLeft]);
while (mainLeft &amp;lt; arr.length) {
    if (right &amp;lt; arr.length) {
        if ((!(arr[subLeft] === arr[right])) &amp;amp; !subStrArr.includes(arr[right])) {
            subStrArr.push(arr[right]);
            right++;
        } else {
            if (subStrArr.length &amp;gt; subStr) subStr = subStrArr.length;
            subStrArr = [];
            subLeft = right;
            subStrArr.push(arr[subLeft]);
            right++;
        }
    } else {
        if (subStrArr.length &amp;gt; subStr) subStr = subStrArr.length;
        mainLeft++;
        subStrArr = [];
        subLeft = mainLeft;
        subStrArr.push(arr[subLeft]);
        right = subLeft + 1;
    }
}
return subStr;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;br&gt;
At first glance, it’s messy—but each part has a purpose. Here’s the breakdown:&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Preparing the String&lt;br&gt;
let arr = str.split('')&lt;br&gt;
I converted the string into an array of characters to make it easier to access individual elements. For example, "abc" becomes ['a', 'b', 'c'].&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Defining Pointers&lt;br&gt;
let mainLeft = 0;&lt;br&gt;
let subLeft = 0;&lt;br&gt;
let right = subLeft + 1;&lt;br&gt;
• mainLeft: Keeps track of where to start exploring new substrings.&lt;br&gt;
• subLeft: Marks the start of the current substring without repeats.&lt;br&gt;
• right: Moves forward to expand the substring until a repeat is found.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Tracking Substrings&lt;br&gt;
let subStrArr = [];&lt;br&gt;
let subStr = 0;&lt;br&gt;
subStrArr.push(arr[subLeft]);&lt;br&gt;
• subStrArr keeps the current substring we are building.&lt;br&gt;
• subStr stores the length of the longest substring found so far.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Iterating Through the String&lt;br&gt;
I used a while loop to explore all possible substrings starting from mainLeft. Inside, I either expand the substring or reset it when a repeated character shows up.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Expanding and Resetting&lt;br&gt;
When the current character isn’t in subStrArr, I add it and move right forward. If it’s a repeat, I check if the current substring is the longest, reset, and start from the next position.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Reflection&lt;/strong&gt;&lt;br&gt;
The solution worked for most cases, but it failed on the load test. Why?&lt;br&gt;
• Using an array and includes check is O(n) for each character.&lt;br&gt;
• Nested iterations make the worst-case O(n²).&lt;br&gt;
For large strings, this approach becomes inefficient.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;The Efficient Solution:&lt;/strong&gt; Sliding Window&lt;br&gt;
Here’s the optimized solution I found on the LeetCode discussion:&lt;br&gt;
var lengthOfLongestSubstring = function (s) {&lt;br&gt;
  let set = new Set();&lt;br&gt;
  let left = 0;&lt;br&gt;
  let maxLen = 0;&lt;/p&gt;

&lt;p&gt;for (let right = 0; right &amp;lt; s.length; right++) {&lt;br&gt;
    while (set.has(s[right])) {&lt;br&gt;
      set.delete(s[left]);&lt;br&gt;
      left++;&lt;br&gt;
    }&lt;br&gt;
    set.add(s[right]);&lt;br&gt;
    maxLen = Math.max(maxLen, right - left + 1);&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;return maxLen;&lt;br&gt;
};&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Sliding Window:
o   We keep a “window” [left, right] of non-repeating characters.
o   left moves forward if a duplicate is found.&lt;/li&gt;
&lt;li&gt; Using a Set:
o   set.has() and set.delete() are O(1), so we avoid scanning the substring repeatedly.&lt;/li&gt;
&lt;li&gt; Expanding and Contracting:
o   Move right forward to include new characters.
o   If a repeat is found, remove characters from left until it’s gone.&lt;/li&gt;
&lt;li&gt; Updating Maximum Length:&lt;/li&gt;
&lt;li&gt; maxLen = Math.max(maxLen, right - left + 1);
o   This calculates the current substring length and updates the maximum.
________________________________________&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Complexity&lt;/strong&gt;&lt;br&gt;
• Time Complexity: O(n) → each character is processed at most twice.&lt;br&gt;
• Space Complexity: O(min(n, m)) → m is the character set size (e.g., 128 for ASCII).&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Solving this problem was an eye-opener. My first solution worked but wasn’t efficient. The sliding window approach not only passes all test cases but also handles large strings efficiently.&lt;br&gt;
This exercise reinforced the importance of:&lt;br&gt;
• Using appropriate data structures&lt;br&gt;
• Thinking about time complexity&lt;br&gt;
• Sliding window technique for substring problems&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>learning</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
