Longest Substring Without Repeating Characters

Sliding Window / 2 Pointer Longest and Smallest Window Problems Hard
  • This problem statement is a fundamental concept of string parsing used in many applications, such as web browsers and text editors
  • Specifically, it is used in web security
  • For example, in JWT (JSON Web Tokens) used for securely transmitting information between parties as a JSON object, there is a need to detect duplication or repetition in the token string
  • This technique is also used for creating unique user session IDs and securing password hashes by detecting repeating character sequences
  • On a lighter side, in gaming, this algorithm can help design games with code-breaking themes, where players need to identify non-repeating sequences

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