</>

3. Longest Substring Without Repeating Characters

Medium NeetCode 150
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Sliding Window
Time
—
Space
—

Standard Solution (C++)

SummaryGiven a string, find the length of the longest contiguous substring in which no character appears more than once.

Approach
  • Use a sliding window defined by two pointers (left, right) that always brackets a duplicate-free substring.
  • Keep a hash map from each character to its most-recently-seen index so we can jump the left boundary forward in O(1) instead of crawling one step at a time.
  • When right lands on a character already inside the window (its stored index >= left), advance left past that earlier occurrence, eliminating the duplicate instantly.
  • Update the answer after every step; total work is O(n) because each character is visited at most twice (once by each pointer).
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int> lastSeen; // char -> most recent index
        int best = 0;

        // [left, right] is the current duplicate-free window
        for (int left = 0, right = 0; right < (int)s.size(); ++right) {
            char c = s[right];

            // If c is inside the current window, shrink from the left
            auto it = lastSeen.find(c);
            if (it != lastSeen.end() && it->second >= left)
                left = it->second + 1;

            lastSeen[c] = right;
            best = max(best, right - left + 1);
        }

        return best;
    }
};
Time O(n) Space O(min(n, A)) where A is the alphabet size (at most 128 for ASCII, effectively O(1))

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;

class Solution {
   public:
    int lengthOfLongestSubstring(string s) {
        int len = s.length();
        int maxLen = 0;
        unordered_map<char, int> umap;  // store the last seen index of each char
        int left = 0;

        for (int right = 0; right < len; ++right) {
            char cur = s[right];
            if (umap.count(cur) && umap[cur] >= left) {  // note that `umap[cur]>=left` is important to keep `left` not going back
                left = umap[cur] + 1;
            }
            maxLen = std::max(maxLen, right - left + 1);
            umap[cur] = right;
        }

        return maxLen;
    }
};

// Time: O(n) on average
// Space: O(1), since the size of ASCII code (256) is often consider constant.

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern