AlgoMaster Logo

Longest Substring Without Repeating Characters

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force

Intuition:

The most straightforward way to solve this problem is by checking each possible substring to see if it contains all unique characters. We can iterate over all possible starting points and ending points for substrings and check for uniqueness.

Code:

2. Sliding Window using HashSet

Intuition:

To reduce the complexity, we can use a sliding window technique. Instead of checking every substring, we can maintain a window and move it to the right while ensuring all characters within the window are unique.

Code:

3. Sliding Window using HashMap

Intuition:

Further optimization can be done by optimizing the move of the start pointer. We can use a hash map to track the last seen index of each character, which allows skipping over sections of the string already known to contain non-unique characters.

Code: