-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion32.java
More file actions
28 lines (23 loc) · 836 Bytes
/
question32.java
File metadata and controls
28 lines (23 loc) · 836 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
import java.util.HashSet;
public class question32 {
public static int lengthOfLongestSubstring(String sc) {
int n = sc.length();
int maxLength = 0;
int left = 0;
HashSet<Character> set = new HashSet<>();
for (int right = 0; right < n; right++) {
while (set.contains(sc.charAt(right))) {
set.remove(sc.charAt(left));
left++;
}
set.add(sc.charAt(right));
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
public static void main(String[] args) {
String s = "abcabcbb";
int result = lengthOfLongestSubstring(s);
System.out.println("Length of Longest Substring Without Repeating Characters: " + result);
}
}