In this problem ,we have to determine that the longest substring is present in the given string (but the characters must not be repeat).

Look at these examples :

Example 1:
Input: “abcabcbb”
Output: 3
Explanation: The longest substring without repeating characters are “abc”, “bca”, and “cab” with lengths of 3

Example 2:
Input: “brainmentors”
Output: 7
Explanation: The longest substrings without repeating characters are “brainme” and “mentors”, with lengths of 7

we solve this problem using Sliding window technique with HashSet :

Step 1:Let's take a HashSet and two variables one(Max_length) is for storing the length of a substring and  second one(i) is used as a pointer which works as a left pointer,both are initialized by value "0".

Step 2:Now ,using for loop we iterates the right side pointer.In this loop ,we traverse the given string from start to last index.

Steps 3:If  !set.contains(string.charAt(j)) ,then we have to add the jth index char in the HashSet:- set.add(string.charAt(j)) .

And also updates the value of Max_length = Math.max(Max_length,j-i+1).

Steps 4:Else , while string.charAt(i) != string.charAt(i) ,then remove ith index value from the set and update by +1 .Then, remove ith remove ith index char from the set and update by +1 and add the jth index char in the set.

Steps 5:In the last ,print the value of Max_length.

Code for this problem:

Output:

Note:Time Complexity of this problem by using this method is O(n),Where n is the number of characters(length of string) present in the given string.