3. Longest Substring Without Repeating Characters

 

Mastering the Longest Substring Without Repeating Characters Problem Using Sliding Window and Set

Introduction

Welcome to another deep dive into solving classic algorithm problems! Today, we’re tackling the Longest Substring Without Repeating Characters problem. This is a common problem that tests your understanding of data structures and algorithmic techniques. We’ll explore an efficient solution using the sliding window technique combined with a Set. This approach is both intuitive and powerful, enabling us to solve the problem efficiently.

Problem Description

Given a string s, find the length of the longest substring without repeating characters. Essentially, we need to find the maximum length of a substring where no characters repeat.

Intuition

To solve this problem, we need to maintain a window (substring) where all characters are unique. The sliding window technique allows us to dynamically adjust the size of this window and ensure it contains only unique characters. We use a Set to keep track of the characters in the current window efficiently.

Approach

  1. Initialize Variables:

    • result: Stores the length of the longest substring found so far.

    • left: Represents the starting index of the current window.

    • charSet: A Set to store characters in the current window, ensuring no duplicates.

  2. Expand the Window:

    • Iterate through the string with a for-loop.

    • For each character s[i], check if it's already in charSet.

    • If it is, remove characters from the left side of the window until s[i] can be added without duplicates.

  3. Update Results:

    • Add s[i] to charSet.

    • Update result to the maximum length found so far.

  4. Return Result:

    • Return result after processing the entire string.

Code Implementation

Here’s the JavaScript code that efficiently finds the length of the longest substring without repeating characters:

javascript
var lengthOfLongestSubstring = function(s) {
    let result = 0;
    let left = 0;
    let charSet = new Set();
    for(let i = 0; i < s.length; i++) {
        while(charSet.has(s[i])) {
            charSet.delete(s[left]);
            left++;
        }
        charSet.add(s[i]);
        result = Math.max(result, i - left + 1);
    }
    return result;
};

Highlight: Sliding Window Technique

The sliding window technique is a powerful tool for solving problems involving contiguous segments of an array or string. By dynamically adjusting the window size, we can efficiently handle a wide range of problems.

In this problem, the sliding window technique allows us to maintain a window where all characters are unique. We expand the window by moving the right pointer (i), and contract it by moving the left pointer (left) when we encounter a duplicate character.

Highlight: Use of Set

The Set data structure is crucial in this solution. It allows us to efficiently check for duplicates and remove characters, ensuring the operations are performed in constant time on average.

  • Checking for Duplicates: Using the Set, we can quickly check if a character is already in the current window.

  • Removing Characters: The Set enables us to efficiently remove characters from the left side of the window when necessary.

Complexity Analysis

  • Time Complexity: The time complexity is O(n)O(n), where nn is the length of the string. Each character is processed at most twice, once when added and once when removed from the Set.

  • Space Complexity: The space complexity is O(min(n,m))O(min(n, m)), where mm is the size of the character set (e.g., 26 for lowercase letters), as the Set stores distinct characters in the window.

Conclusion

The sliding window technique, combined with a Set, provides an efficient and elegant solution to the Longest Substring Without Repeating Characters problem. By mastering this approach, you’ll be better equipped to tackle a variety of algorithmic challenges.

I hope you found this exploration insightful! If you have any questions or additional insights, feel free to share in the comments.

Happy coding! 😊

LeetCode Solution Link : https://leetcode.com/problems/longest-substring-without-repeating-characters/solutions/6168369/solution-using-set-in-js-by-bikash_kumar-ukcf/

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix