- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestUniqueSubstring.java
More file actions
Latest commit
32 lines (25 loc) · 871 Bytes
/
Copy pathLongestUniqueSubstring.java
File metadata and controls
32 lines (25 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
packagecom.sarvesh.javabasics;
importjava.util.HashSet;
publicclassLongestUniqueSubstring {
publicstaticvoidmain(String[] args) {
Stringstream = "abcabcbb";
intmaxLength = lengthOfLongestSubstring(stream);
System.out.println("Longest unique packet sequence length: " + maxLength);
}
publicstaticintlengthOfLongestSubstring(Strings) {
HashSet<Character> set = newHashSet<>();
intleft = 0;
intright = 0;
intmaxLength = 0;
while (right < s.length()) {
charcurrentChar = s.charAt(right);
while ( set.contains(currentChar)) {
set.remove(s.charAt(left));
left++;
}
maxLength = Math.max(maxLength,right-left+ 1);
right++;
}
returnmaxLength;
}
}