Longest Substring Without Repeating Characters

Posted: 19 Sep, 2020
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

Given a string 'S' of length 'L', return the length of the longest substring without repeating characters.

Example:

Suppose given input is "abacb", then the length of the longest substring without repeating characters will be 3 ("acb").
Input Format:
Each input contains a single line which contains the string 'S', Where 'S' denotes the input string containing english letters ( both UpperCase and LowerCase), digits, symbols, and spaces.
Output Format:
Print a single integer denoting the length of the longest substring without repeating characters.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
0 <= L <= 10^5     

Time limit: 1 sec
Follow Up:
Can you solve this problem in O(L) time and space complexity?
Approach 1

In the brute force approach, we will use two nested loops. The outer loop is used to select the starting index of the substring and the inner loop is used to fix the ending index of the substring. After selecting the substring, we will use another loop (or a method) to check whether the substring contains all unique characters or not using a HashSet.

Try Problem