Prepin
Log in

Identify the longest substring that does not contain any repeating characters.

Approach / Explanation

The task is to identify the longest substring within a given string where all characters are unique, meaning each character appears only once. A substring is defined as a consecutive sequence of characters from the original string. To tackle this issue, techniques such as sliding window, hashing or a set data structure, along with a two-pointer approach, can be employed. To address this problem, follow these steps: Start by initializing two pointers at the beginning of the string. Expand the window by moving the right pointer and adding the character to the hash table or set. If a duplicate character is encountered, remove characters from the left until there are no duplicates remaining in the window. The maximum length of the window throughout this process will represent the length of the longest substring without repeating characters.

Suggested Answer

For instance, if the provided string is "abcdabcbb", the longest substring that does not contain repeating characters is "abcd," which has a length of 4. Here is a potential Python solution utilizing a set data structure: def lengthOfLongestSubstring ( s ) : window_chars_map = { } left = 0 max_length = 0 for right in range ( len ( s ) ) : if s [ right ] in window_chars_map : left = max ( left , window_chars_map [ s [ right ] ] + 1 ) window_chars_map [ s [ right ] ] = right max_length = max ( max_length , right - left + 1 ) return max_length This solution employs a dictionary to monitor the characters present in the current window (from index left to right). If a duplicate character is encountered, the left pointer is adjusted to exclude this character in the upcoming window. This procedure continues until all characters in s have been processed.

Alternative Answer

With the string "abcdabcbb", the longest substring that contains no repeating characters is "abcd", which has a length of 4. This can be implemented in JavaScript using a similar method: var lengthOfLongestSubstring = function ( s ) { var start = 0 , maxLen = 0 ; var map = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { var ch = s [ i ] ; if ( map . get ( ch ) >= start ) start = map . get ( ch ) + 1 ; map . set ( ch , i ) ; if ( i - start + 1 > maxLen ) maxLen = i - start + 1 ; } return maxLen ; } ; In this implementation, we utilize a JavaScript Map to keep track of each character in the current segment (from index start to i). When a duplicate character is encountered, we adjust the start pointer to the right of the previously found character in that segment by updating start to map.get(ch) + 1. We also update maxLen whenever we identify a longer valid substring.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Microsoft

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.