Longest Substring Without Repeating Characters

Sliding Window / 2 Pointer Longest and Smallest Window Problems Hard

Given a string, S. Find the length of the longest substring without repeating characters.

Examples:

Input : S = "abcddabac"

Output : 4

Explanation : The answer is "abcd" , with a length of 4.

Input : S = "aaabbbccc"

Output : 2

Explanation : The answers are "ab" , "bc". Both have maximum length 2.

Input : S = "aaaa"

Constraints

  • 1 <= S.length <= 5*104
  • S contains only English lowercase letters.

Hints

  • Use a hash map (or set) to store the characters currently in the window. For each character, If it's not in the hash map, add it to the window and update the maximum length. If it's already in the hash map, move the left pointer forward until the duplicate is removed.
  • After processing each character, compute the length of the current window as right−left+1. Keep track of the maximum length encountered during the traversal.

Company Tags

Dropbox Shopify Snowflake Johnson & Johnson Wayfair Micron Technology Ubisoft Bungie Rakuten Swiggy NVIDIA Roblox Unity Technologies Stripe Chewy Target Rockstar Games Bloomberg McKinsey & Company Zoho Twilio HCL Technologies PayPal ARM Qualcomm Google Microsoft Amazon Meta Apple Netflix Adobe