Advertisement

Longest Substring Without Repeating Characters - LeetCode 3 Solution

Longest Substring Without Repeating Characters - Complete Solution Guide

Longest Substring Without Repeating Characters is LeetCode problem 3, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s , find the length of the longest substring without duplicate characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Note that "bca" and "cab" are also correct answers. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence

Detailed Explanation

The problem asks us to find the length of the longest substring within a given string that contains no repeating characters. A substring is a contiguous sequence of characters within a string. For example, in the string "abcabcbb", "abc" is a substring of length 3 without repeating characters, while "pwke" is a subsequence (not contiguous) of the string "pwwkew", and we're looking for substrings. The input is a string `s`, and the output is an integer representing the length of the longest substring without repeating characters. The string `s` can contain English letters, digits, symbols, and spaces, and its length can be between 0 and 5 * 10^4.

Solution Approach

The solution uses a sliding window technique along with a hash table (or array) to keep track of the last seen index of each character. The window expands to the right, and the hash table is updated with the character's index. If a repeating character is encountered within the current window, the left boundary of the window is moved to the right of the previous occurrence of that character. The maximum length of the substring without repeating characters is updated at each step.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash table (or array) `char_map` to store the last seen index of each character. Initialize the left pointer `left` to 0 and the maximum length `max_length` to 0.
  2. Step 2: Iterate through the string `s` from left to right using the right pointer `right`.
  3. Step 3: For each character `char` at index `right`, check if it exists in `char_map` and if its last seen index is within the current window (i.e., `char_map[char] >= left`).
  4. Step 4: If the character is a duplicate within the current window, update the left pointer `left` to `char_map[char] + 1` to exclude the previous occurrence of the character from the current window.
  5. Step 5: Update `char_map[char]` with the current index `right`.
  6. Step 6: Update `max_length` with the maximum of the current `max_length` and the current window length (`right - left + 1`).
  7. Step 7: After iterating through the entire string, return `max_length`.

Key Insights

  • Insight 1: The problem can be solved efficiently using a sliding window approach.
  • Insight 2: A hash table (or character array in the C version) can be used to keep track of the characters within the current window and their last seen positions.
  • Insight 3: When a repeating character is encountered, the left boundary of the window should be moved to the right of the previous occurrence of that character.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, String, Sliding Window.

Companies

Asked at: AMD, Accenture, Accolite, Adobe, Agoda, Airtel, Alibaba, Amazon, American Express, Apple, Atlassian, BNY Mellon, Bloomberg, ByteDance, Capgemini, Cisco, Cognizant, Comcast, Coupang, Dell, Deloitte, Docusign, EPAM Systems, Epic Systems, Expedia, Flipkart, Freecharge, FreshWorks, Goldman Sachs, Google, Grab, HCL, HPE, IBM, Infosys, Intel, Intuit, J.P. Morgan, Juspay, LinkedIn, Lucid, Lyft, MAQ Software, MakeMyTrip, Meta, Microsoft, Morgan Stanley, NCR, Nagarro, Netflix, Nike, Nvidia, Optum, Oracle, Ozon, Palo Alto Networks, PayPal, Paytm, PornHub, Publicis Sapient, Qualcomm, Roblox, SOTI, Salesforce, ServiceNow, Snowflake, Softwire, Spotify, Tesla, ThoughtWorks, TikTok, Tinkoff, Turing, Uber, Veeva Systems, Virtusa, Visa, Walmart Labs, Wipro, Yahoo, Yandex, Yelp, ZScaler, Zeta, Zoho, Zynga, athenahealth, eBay, josh technology, opentext, persistent systems, tcs.