import java.util.*;
class NoRepeatSubstring {
public static int findLength(String str) {
char[] a = str.toCharArray();
HashSet<Character> set = new HashSet<>();
int j =0;
set.add(a[j]);
int maxSubStringLen = Integer.MIN_VALUE;
maxSubStringLen = Math.max(maxSubStringLen, set.size());
j++;
for (;j < a.length; j++){
Character cj = a[j];
if (!set.contains(cj)){
set.add(cj);
maxSubStringLen = Math.max(maxSubStringLen, set.size());
} else {
set.clear(); // clear the set if there is a match and start counting again
set.add(cj);
}
}
return maxSubStringLen;
}
public static void main(String[] args) {
System.out.println("Length of the longest substring: " + NoRepeatSubstring.findLength("aabccbb"));
System.out.println("Length of the longest substring: " + NoRepeatSubstring.findLength("abbbb"));
System.out.println("Length of the longest substring: " + NoRepeatSubstring.findLength("abccde"));
}
}