Tutorialspoint
Problem
Solution
Submissions

Longest Substring Without Repeating Characters

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

Write a C 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 substring "abc" has a length of 3 with no repeating characters. There are several substrings without repeating characters: "a", "b", "c", "ab", "bc", "ca", "abc", etc. Among these, "abc" is one of the longest with a length of 3. Therefore, the output is 3.
Example 2
  • Input: s = "bbbbb"
  • Output: 1
  • Explanation: The longest substring without repeating characters is "b" with a length of 1. All other substrings of length greater than 1 contain repeating characters. Therefore, the output is 1.
Constraints
  • 0 ≤ s.length ≤ 5 * 10^4
  • s consists of English letters, digits, symbols and spaces
  • Time Complexity: O(n), where n is the length of the string
  • Space Complexity: O(min(m,n)), where m is the size of the character set and n is the length of the string
StringsAccentureZomato
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 a sliding window approach to track the current substring without repeating characters
  • Maintain a set or hash table to check if a character exists in the current substring
  • Use two pointers to represent the start and end of the current substring
  • When you encounter a repeating character, move the start pointer to the right of the previous occurrence of that character
  • Keep track of the maximum length encountered so far

Steps to solve by this approach:

 Step 1: Initialize variables to track the start of the window, current length, and maximum length.

 Step 2: Create an array or hash table to store the last position of each character.
 Step 3: Iterate through the string character by character.
 Step 4: For each character, check if it has been seen before and is within the current window.
 Step 5: If the character is a repeat in the current window, update the start position to the right of the last occurrence.
 Step 6: Update the position of the current character in the hash table.
 Step 7: Calculate the current window length and update the maximum length if needed.

Submitted Code :