Code With Coffie
  • HOME
  • ABOUT US
  • PORTFOLIO
  • AI
    • AGENTIC AI
    • Generative AI
    • LangChain
    • LangGraph
    • LLM
    • MCP
    • RAG
  • TUTORIAL
    • MYSQL
      • DATETIME
    • DSA
      • LEETCODE
    • GIT
    • Docker
    • INTERVIEW
    • PROGRAMME
      • STAR PATTERN PROGRAMME
  • PYTHON
    • DJANGO
    • FLASK
    • FastAPI
    • Matplotlib
    • NumPy
    • Pandas
    • STREAMLIT
  • JAVASCRIPT
    • Vue.js
  • PHP
    • PHP OOPS
    • LARAVEL
    • WORDPRESS
  • NEXTERP
  • Home
  • Blog
  • LeetCode
  • LeetCode 3: Longest Substring Without Repeating Characters

LeetCode 3: Longest Substring Without Repeating Characters

Oct 04, 2026 by codewithhemu
LeetCode 3PHPMediumSliding WindowHash Map

LeetCode Problem 3, Longest Substring Without Repeating Characters, is a popular coding interview question that helps developers understand strings, Hash Maps, and the Sliding Window technique.

In this tutorial, we will solve the problem using PHP, understand the brute-force approach, optimize it using Sliding Window, and walk through the solution with detailed examples and a dry run.

1. Problem Statement

Given a string s, find the length of the longest substring without repeating characters.

A substring is a continuous sequence of characters within a string. Every character in the required substring must be unique.

Example 1

InputExample
s = "abcabcbb"

Output: 3

Explanation: The longest substring without repeating characters is "abc", which has a length of 3. Other valid answers include "bca" and "cab".

Example 2

Input
s = "bbbbb"

Output: 1

Explanation: Every character is the same, so the longest valid substring is "b", with a length of 1.

Example 3

Input
s = "pwwkew"

Output: 3

Explanation: The longest valid substring can be "wke" or "kew", both with a length of 3. Note that "pwke" is not a substring because its characters are not consecutive.

2. Understanding Substrings

A substring is a sequence of characters that appear consecutively in the original string.

Original string: abcde

abcde

“abc” is a substring because the characters are consecutive.


abcde

“ace” is a subsequence, not a substring, because it skips characters.

This difference is important. The problem asks for a substring, which means we cannot skip any characters between the beginning and end of the selected range.

3. Brute Force Approach

The brute-force approach checks all possible substrings and finds the longest one that contains no duplicate characters.

For every starting index, we expand the substring character by character. We store encountered characters in a temporary array. If a duplicate appears, we stop expanding from that starting point.

PHP Code – Brute Force

PHPBrute Force
class Solution {
    function lengthOfLongestSubstring($s) {
        $n = strlen($s);
        $maxLength = 0;

        for ($i = 0; $i < $n; $i++) {
            $seen = [];

            for ($j = $i; $j < $n; $j++) {
                $char = $s[$j];

                if (isset($seen[$char])) {
                    break;
                }

                $seen[$char] = true;

                $length = $j - $i + 1;
                $maxLength = max($maxLength, $length);
            }
        }

        return $maxLength;
    }
}

How the Brute Force Approach Works

Consider the input "abcabcbb".

  • Start at index 0 and form "a", "ab", and "abc". Stop when another "a" appears.
  • Start at index 1 and form "b", "bc", and "bca". Stop when "b" repeats.
  • Continue from the remaining starting positions and update the maximum valid length.
Limitation: The brute-force approach can take O(n²) time in the worst case because it explores multiple substrings. We can optimize this using the Sliding Window technique.

4. Optimized Approach – Sliding Window

The Sliding Window technique maintains a continuous range of characters and adjusts that range as we traverse the string. Instead of restarting the search for every character, we move two pointers through the string.

  • Left pointer: The starting index of the current window.
  • Right pointer: The ending index of the current window.
  • Hash Map: Stores the most recent index of each character.

When a repeated character is found inside the current window, we move the left pointer to one position after its previous occurrence.

Sliding Window Visualization

String: “abcabcbb”

Current window: “abc”

abcabcbb
01234567

Left = 0 | Right = 2 | Length = 3

The current window contains "abc", and all characters are unique. When the next character "a" is encountered at index 3, the left pointer moves to index 1, making the new window "bca".

5. Step-by-Step Algorithm

  1. Initialize left = 0 and maxLength = 0.
  2. Create an empty Hash Map named lastSeen to store the last index of each character.
  3. Traverse the string using the right pointer.
  4. For each character, check if it exists in the Hash Map and if its previous index is inside the current window.
  5. If a duplicate is inside the window, move left to the index after that previous occurrence.
  6. Update the character’s latest index in the Hash Map.
  7. Calculate the current window length and update maxLength.
  8. Return maxLength after processing the string.

6. Complete Optimized PHP Solution

The following solution uses Sliding Window and a Hash Map to achieve linear time complexity.

PHPOptimized Solution
class Solution {
    function lengthOfLongestSubstring($s) {
        $lastSeen = [];
        $left = 0;
        $maxLength = 0;
        $n = strlen($s);

        for ($right = 0; $right < $n; $right++) {
            $char = $s[$right];

            if (isset($lastSeen[$char]) &&
                $lastSeen[$char] >= $left) {
                $left = $lastSeen[$char] + 1;
            }

            $lastSeen[$char] = $right;

            $currentLength = $right - $left + 1;

            $maxLength = max(
                $maxLength,
                $currentLength
            );
        }

        return $maxLength;
    }
}

7. Explaining the Code Line by Line

Step 1: Initialize Variables

PHP
$lastSeen = [];
$left = 0;
$maxLength = 0;
$n = strlen($s);

$lastSeen stores the most recent position of each character. $left marks the start of the current window, and $maxLength stores the longest valid substring length found so far.

Step 2: Traverse the String

PHP
for ($right = 0; $right < $n; $right++) {
    $char = $s[$right];
}

The right pointer moves through the string one character at a time, identifying the current character.

Step 3: Check for Repeated Characters

PHP
if (isset($lastSeen[$char]) &&
    $lastSeen[$char] >= $left) {
    $left = $lastSeen[$char] + 1;
}

This is the most important part of the algorithm.

  • isset() checks whether the character has been encountered before.
  • $lastSeen[$char] >= $left confirms that the previous occurrence lies inside the current window.
  • $left = $lastSeen[$char] + 1 moves the left pointer beyond the previous occurrence.

Step 4: Update the Hash Map

PHP
$lastSeen[$char] = $right;

We store the current index as the latest position of the character. This makes future duplicate checks accurate.

Step 5: Calculate the Window Length

PHP
$currentLength = $right - $left + 1;
$maxLength = max($maxLength, $currentLength);

The current window length is calculated by subtracting the left index from the right index and adding 1. We then update the maximum length if the current window is longer.

Step 6: Return the Result

PHP
return $maxLength;

After the loop has processed all characters, the maximum length is the answer.

8. Detailed Dry Run

Let’s execute the optimized code with the input "abcabcbb".

RightCharacterLeftCurrent WindowLengthMax Length
0a0a11
1b0ab22
2c0abc33
3a1bca33
4b2cab33
5c3abc33
6b5cb23
7b7b13

Understanding the Important Iterations

Iteration 1: The first character is a at index 0. The current window is "a", so the maximum length is 1.

Iteration 2: The character b is new. The current window becomes "ab", with a length of 2.

Iteration 3: The character c is also new. The window becomes "abc", and the maximum length becomes 3.

Iteration 4: The character a repeats at index 3. Its previous index was 0, so we move left to 1. The current window becomes "bca".

Iteration 5: The character b repeats at index 4. Its previous index was 1, so we move left to 2. The window becomes "cab".

Iteration 6: The character c repeats. Its previous index was 2, so we move left to 3. The window becomes "abc".

Iteration 7: The character b repeats at index 6. Its previous index was 4, so we move left to 5. The window becomes "cb".

Iteration 8: The character b repeats again. Its previous index is 6, so we move left to 7. The window becomes "b".

Final Answer: 3
The longest substring without repeating characters has a length of 3.

9. Why Do We Check lastSeen[char] >= left?

This condition prevents the left pointer from moving backwards when a character repeats outside the current window.

Consider the string "abba".

IndexCharacterLeftWindowExplanation
0a0aFirst character
1b0abNew character
2b2bDuplicate found; move left
3a2baOld a is outside current window

At index 3, the previous occurrence of a was at index 0, but the current window starts at index 2. Since index 0 is outside the current window, we must not move the left pointer backwards.

Interview tip: Always make sure that the previous occurrence of a repeated character is within the current window before updating the left pointer.

10. Edge Cases

InputOutputExplanation
""0Empty string
"a"1Only one character
"bbbbb"1All characters repeat
"abcdef"6All characters are unique
"abba"2Repeated characters occur
"pwwkew"3Longest unique substring is wke or kew

11. Time and Space Complexity

Time Complexity: O(n)

The right pointer traverses the string once. The left pointer only moves forward, so each character is processed a constant number of times. Therefore, the optimized solution has linear time complexity.

Space Complexity: O(min(n, k))

The Hash Map stores the latest index of each distinct character. Here, n is the string length and k is the size of the possible character set. For the problem’s ASCII character set, the Hash Map has a bounded number of entries.

12. Common Mistakes to Avoid

  • Confusing substring and subsequence: A substring must contain consecutive characters.
  • Moving the left pointer backwards: Check that the previous occurrence is at or beyond the current left pointer.
  • Not using the latest occurrence: Store the most recent index of each character.
  • Updating maximum length too early: Adjust the window first, then calculate its length.
  • Using unnecessary nested loops: Sliding Window is more efficient for this problem.

13. Frequently Asked Questions

Q1. What is LeetCode Problem 3?

It is a string problem where you need to find the length of the longest substring that contains no repeating characters.

Q2. Which algorithm is used to solve this problem?

The optimized solution uses the Sliding Window technique with a Hash Map to track the last-seen index of each character.

Q3. What is the time complexity of the optimized solution?

The time complexity is O(n), where n is the length of the string.

Q4. Why do we use a Hash Map?

A Hash Map allows us to efficiently store and retrieve the most recent index of a character so we can quickly adjust the window when a duplicate is found.

Q5. Can this problem be solved without a Hash Map?

Yes. Other data structures, such as a fixed-size array for a known character set, can also track character positions. A Hash Map is a flexible choice for this solution.

Q6. What is the difference between Sliding Window and Brute Force?

Brute Force repeatedly checks substrings and can take O(n²) time. Sliding Window reuses the current range and solves the problem in O(n) time.

Q7. Does this solution work with Unicode characters?

The given PHP solution uses byte-based string indexing, suitable for the ASCII character set described by the problem. Unicode text requires multibyte-aware handling.

14. Conclusion

LeetCode 3: Longest Substring Without Repeating Characters is an important coding interview problem for learning the Sliding Window technique and Hash Maps.

The brute-force approach helps us understand how substrings work, but the optimized Sliding Window approach eliminates repeated work by maintaining a valid range of unique characters.

Key Takeaways
  • Use two pointers to maintain a current substring window.
  • Use a Hash Map to store the last-seen index of each character.
  • Move the left pointer only when a duplicate lies within the current window.
  • Update the maximum length after adjusting the window.
  • The optimized solution takes O(n) time.

Understanding this pattern will help you solve many other string and array problems commonly asked in technical interviews.

  • Share:
Previous Article LeetCode 4: Median of Two Sorted Arrays in PHP
Next Article LeetCode 2: Add Two Numbers in PHP | Step-by-Step Solution
No comments yet! You be the first to comment.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

category

  • AGENTIC AI (2)
  • DATETIME (6)
  • DJANGO (1)
  • Docker (1)
  • DSA (22)
  • DSA PRACTICE (4)
  • GIT (1)
  • INTERVIEW (3)
  • JAVASCRIPT (69)
  • LARAVEL (41)
  • LeetCode (4)
  • MYSQL (45)
  • PHP (21)
  • PHP OOPS (16)
  • PROGRAMME (1)
  • PYTHON (11)
  • RAG (5)
  • REACT JS (6)
  • STAR PATTERN PROGRAMME (7)
  • Uncategorized (21)
  • Vue.js (5)
  • WORDPRESS (15)

Archives

  • October 2026
  • September 2026
  • July 2026
  • June 2026
  • May 2026
  • March 2026
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • January 2023

Tags

Certificates Education Instructor Languages School Member

Building reliable software solutions for modern businesses. Sharing practical tutorials and real-world project insights to help developers grow with confidence.

GET HELP

  • Home
  • Portfolio
  • Privacy Policy
  • Terms & Conditions
  • Disclaimer
  • Contact Us

PROGRAMS

  • Software Development
  • Performance Optimization
  • System Architecture
  • Project Consultation
  • Technical Mentorship

CONTACT US

  • Netaji Subhash Place (NSP) Delhi
  • Tel: + (91) 8287315524
  • Email: contact@codewithcoffie.com

Copyright © 2026 LearnPress LMS | Powered by LearnPress LMS