Tutorialspoint
Problem
Solution
Submissions

Longest Substring Without Repeating Characters

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a JavaScript program to find the length of the longest substring without repeating characters in a given string. A substring is a contiguous sequence of characters within a string.

Example 1
  • Input: s = "abcabcbb"
  • Output: 3
  • Explanation:
    • The string "abcabcbb" contains multiple substrings without repeating characters.
    • The longest substring without repeating characters is "abc" with length 3.
    • Other substrings like "bca" and "cab" also have length 3, but no substring is longer.
    • Therefore, the answer is 3.
Example 2
  • Input: s = "bbbbb"
  • Output: 1
  • Explanation:
    • The string "bbbbb" consists of only one character repeated.
    • The longest substring without repeating characters is "b" with length 1.
    • Any substring longer than 1 would contain repeating characters.
    • Therefore, the answer is 1.
Constraints
  • 0 ≤ s.length ≤ 5 * 10^4
  • s consists of English letters, digits, symbols and spaces
  • Time Complexity: O(n)
  • Space Complexity: O(min(m,n)) where m is the size of the charset
StringsAlgorithmsKPMGPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use the sliding window technique with two pointers (left and right)
  • Maintain a set or map to track characters in the current window
  • Expand the right pointer and add characters to the tracking structure
  • When a duplicate is found, shrink the window from the left until no duplicates exist
  • Keep track of the maximum window size encountered during the process

Steps to solve by this approach:

 Step 1: Initialize variables for maximum length, left pointer, and a set to track characters.
 Step 2: Use right pointer to expand the window by iterating through the string.
 Step 3: If current character is already in the set, shrink window from left until duplicate is removed.
 Step 4: Add current character to the set and update maximum length if current window is larger.
 Step 5: Continue until right pointer reaches the end of the string.
 Step 6: Return the maximum length found during the process.

Submitted Code :