-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLongestSubStrLen.java
More file actions
30 lines (29 loc) · 898 Bytes
/
LongestSubStrLen.java
File metadata and controls
30 lines (29 loc) · 898 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
/* Given a string s, find the length of the longest substring without repeating characters. */
import java.util.*;
public class LongestSubStrLen {
static public int lengthOfLongestSubstring(String s) {
if(s=="") return 0;
int start = 0;
int end = 0;
int max = 0;
Set<Character> set = new HashSet<>();
while (end < s.length()) {
if (!set.contains(s.charAt(end))) {
set.add(s.charAt(end));
end++;
max = Math.max(set.size(), max);
} else {
set.remove(s.charAt(start));
start++;
}
}
return max;
}
public static void main(String args[]){
Scanner s=new Scanner(System.in);
String str = s.nextLine();
int n = lengthOfLongestSubstring(str);
System.out.println(n);
s.close();
}
}